Nested loop in c programming



A loop inside another loop is known as nested loop. We can write any loop inside any loop in c i.e. we can write for loop inside the loop or while loop or do while loop etc. For example:

(a)
#include<stdio.h>
int main(){
int i,j,k;
     for(i=0;i<3;i++){
         for(j=0;j<3;j++){
             printf(" %d",i+j);
         }
    }
return 0;       
}

(b)
#include<stdio.h>
int main(){
int i,j,k;
    do
         while(0)
             for(;0;)
                 printf("cbyexample");
while(0);
return 0;        
}


For loop
While loop
Do while loop
break and continue
C tutorial home.

29 comments:

  1. write a program to poiny the following format 1
    1 1
    12 21
    123 321
    1234 4321
    123454321

    ReplyDelete
    Replies
    1. #include
      #include

      int main()
      {
      int i,j,k;
      for(i=1;i<=5;i++)
      {
      for(j=1;j<=i;j++)
      {
      printf("%d",j);
      }
      if(i<=4)
      printf(" ");


      for(k=i;k>=1;k--)
      {
      if(k<5)
      printf("%d",k);
      }

      printf("\n");
      }
      return 0;
      }

      Delete
  2. how to print
    * *
    ** **
    ******

    ReplyDelete
    Replies
    1. #include
      #include

      int main()
      {
      int i,j,k;
      for(i=1;i<=3;i++)
      {
      for(j=1;j<=i;j++)
      {
      printf("*");
      }
      if(i<3)
      printf(" ");


      for(k=i;k>=1;k--)
      {

      printf("*");
      }

      printf("\n");
      }
      return 0;
      }

      Delete
  3. write program for this format
    0 0 1 0 0
    0 1 0
    1

    ReplyDelete
    Replies
    1. #include
      #include

      int main()
      {
      int i,j,c,m=5;
      for(i=0;i<3;i++)
      {
      c=m/2;
      for(j=0;j<m;j++)
      {
      if(j==c)
      printf("1");
      else
      printf("0");
      }
      m=m-2;
      printf("\n");

      }


      return 0;
      }

      Delete
  4. how to program this using nested while loop
    *
    ***
    *****
    *******
    *********
    ***********

    ReplyDelete
    Replies
    1. #include
      main()

      {
      int n,i,j;

      scanf("%d",&n);

      for (i=0;i<=n;i++)
      {
      for (j=0;j<=i;j++)
      printf("*");
      printf("\n");
      }

      }

      Input- 5

      Delete
  5. how to print
    1
    21
    321
    4321
    54321

    ReplyDelete
    Replies


    1. #include
      #include
      main()
      {int i,j;
      for(i=1;i<=5;i++)
      {for(a=i;a>=i;a--)
      {cout<<a;}
      cout<<"\n;"}
      getch();
      }

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. write a program to print odd and even number between two intervals, which are entered by user?

    ReplyDelete
  8. WAP this program by using nested loop

    *****
    ****
    ***
    **
    *

    ReplyDelete
    Replies
    1. #include
      #include
      int main()
      {
      int n,i,j;
      scanf("%d",&n);
      for(i=1;i<=n;i++)
      {
      for(j=1;j<=n-i+1;j++)
      {
      printf("*");
      }

      printf("\n");
      }
      return(0);
      }

      Delete
  9. How To Program This

    **
    **
    **
    **
    **

    ReplyDelete
    Replies
    1. #include
      int main()
      {
      int i,j,n=2;
      for(j=0;j<5;j++)
      {
      for(i=0;i<n;i++)
      {
      printf("*");
      }
      printf("\n");
      }

      return 0;
      }

      Delete
  10. Enter outer loop:3
    Enter inner loop:5
    5 1 4 2 3 3 2 4 1 5
    5 1 4 2 3 3 2 4 1 5
    5 1 4 2 3 3 2 4 1 5

    ReplyDelete
  11. Hello can you help me in that's one
    1
    12
    123
    1234
    12345
    1234
    123
    12
    1

    ReplyDelete
    Replies
    1. how to generate the output
      N
      NE
      NEP
      NEPA
      NEPAL
      NEPA
      NEP
      NE
      N

      Delete
    2. /program in c++/

      #include
      #include
      main()
      {
      int i,j=1,a=1;
      for(i=1;i<=9;i++)
      {
      while(j<=a)
      {cout<4)
      a--
      else
      a++
      }
      getch();
      }

      Delete
    3. #include
      #include
      main()
      {
      int i,j, n;
      printf("Enter the number of rows");
      scanf("%d",&n);
      for(i=1;i<=n;i++)
      {

      for(j=1;j<=i;j++)
      printf("%d",j);
      printf("\n");
      }
      for(i=1;i<n;i++)
      {

      for(j=1;j<=n-i;j++)
      printf("%d",j);
      printf("\n");
      }
      getch();
      }

      Delete
  12. write a program to print
    1
    121
    12321
    1234321
    123454321
    1234321
    12321
    121
    1

    ReplyDelete

  13. #include
    #include
    main()
    {
    for(int i=1;i<=9;i++)
    {if(i<=5)
    {for(int a=i;a<=i;a++)
    {cout<=2;)
    {b--;cout<<b;}}
    else
    {a--;
    for(c=1;c<a;c++)
    {cout<<c;}
    for(d=a;d<=2;)
    {d--;
    cout<<d;}}
    }
    getch();
    }

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. # # # # # # #
    # # # # # # #
    # # # # # # #
    # # # # # # #
    # # # # # # #
    # # # # # # #
    Figure 1

    NOTE: Use nested loop/s to write your program

    ReplyDelete