Class X Computer science,Nested loops


        SEBA

        COMPUTER SCIENCE

        CHAPTER-5

        NESTED LOOP IN C 

      1. Write C a program to display following patterns Using nested loop construct:

       

      a.

      1 2 3

          1 2 3

          1 2 3

          1 2 3

          1 2 3

      #include<stdio.h>

      int main()

      {

          int i,j;

          for(i=1;i<=5;i++)

          {

              for (j=1;j<=3;j++)

          {

          printf("%d",j);

          }

          printf("\n");

      }

      return 0;

      }

       

       

      b

           1 2 1

          1 2 1

          1 2 1

          1 2 1

          1 2 1

      1st Method

      #include <stdio.h>

      int main()

      {

          int i,j;

          for(i=1;i<=5;i++)

          {

              for (j=1;j<=2;j++)

          {

          printf("%d",j);

          }

          printf("1\n");

      }

      return 0;

      }

       

      2nd  Method

       

      #include <stdio.h>

       

       

      int main()

      {

          int i,j,k;

          for(i=1;i<=5;i++)

          {

              for (j=1;j<=2;j++)

          {

          printf("%d",j);

          }

              for (k=1;k<=1;k++)

          {

              printf("%d",k);

          }

          printf("\n");

      }

      return 0;

      }

       

       

       

       c.

          4 3 2 1

          4 3 2 1

          4 3 2 1

          4 3 2 1

          4 3 2 1

       

      int main()

      {

          int i,j;

          for(i=1;i<=5;i++)

          {

              for (j=4;j>0;j--)

          {

          printf("%d",j);

          }

          printf("\n");

      }

      return 0;

      }

       

       

       

      d.       

                    2

              2     3     4

        2    3     4     5    6

       

      1st method

      #include <stdio.h>

      int main()

      {

          int i,j,k;

          for(i=1;i<=3;i++)

          {

              for (j=1;j<=(3-i);j++)

          {

          printf("   ");

          }

          for(k=1;k<=(2*i-1);k++)

          {

              printf(" %d ",k+1);

          }

          printf("\n");

      }

      return 0;

      }

      2nd method

      #include <stdio.h>

      int main()

      {

          int i,j,k;

          for(i=1;i<=3;i++)

          {

              for (j=1;j<=(3-i);j++)

          {

          printf("   ");

          }

          for(k=2;k<=(2*i);k++)

          {

              printf(" %d ",k);

          }

          printf("\n");

      }

      return 0;

      }

       

       

      e.    

              1

          1   2   1

      1  2   3   2  1

       

      #include <stdio.h>

      int main()

      {

          int i,j;

          for(i=0;i<=3;i++)

          {

              for (j=1;j<=3-i;j++)

          {

          printf("     ");

          }

          for(j=1;j<=i;j++)

          {

              printf("  %d  ",j);

          }

          for(j=i-1; j>=1;j--)

          {

              printf(" %d  ",j);

          }

          printf("\n");

      }

      return 0;

      }

       


      f.

                      *

                 *   *   *

           *    *   *    *    *

      *   *    *    *    *    *   *

           *    *    *    *     *

                 *     *    *

                        *

      #include <stdio.h>

      int main()

      {

          int i,j,n;

          for(i=1;i<=4;i++)

          {

              for (j=0;j<=4-i;j++)

          {

          printf("     ");

          }

          for(n=1;n<=2*i-1;n++)

          {

              printf("  *  ",n);

          }

                   printf("\n");

          }

          for(i=1; i<=4;i++)

          {

             for(j=0;j<=i;j++)

         {

          printf("     ");

         }

      for(n=(2*4-1);n>=(i*2+1);n--)

      {

          printf("  *  ");

      }

          printf("\n");

          }

          return 0;

          }

       

       

       

       

       


       

      g. 

          x                              x

          x      x                      x    x

          x      x      x              x    x     x

         x       x     x      x     x      x    x    x       x      x      x     x      x     x     x    x  x

        #include <stdio.h>

        int main()

       {

          int i,j,n,k;

          for(i=1;i<=5;i++)

          {

              for (j=1;j<=2;j++)

             {

       

              for(n=1;n<=i;n++)

             {

              printf("X  ",n);

             }

          for(k=1;k<=(5-i);k++)

             {

              printf("   ");

             }

       

             }

             printf("\n");

          }

       

          }

          return 0;

      }

       

       

      2. Modify the solution of question no q to accepts the number of lines as input. The program should make the display pattern accordingly. (Hint: write separate program)

       

      a.

      #include<stdio.h>

      int main()

      {

          int i,j,n;

          printf("Enter the number of lines\n");

          scanf("%d",&n);

          for(i=1;i<=n;i++)

          {

              for (j=1;j<=3;j++)

             {

              printf("  %d  ",j);

             }

             printf("\n");

          }

              return 0;

          }

      }

       

      b.

      #include<stdio.h>

      int main()

      {

          int i,j,n;

          printf("Enter the number of lines\n");

          scanf("%d",&n);

          for(i=1;i<=n;i++)

          {

              for (j=1;j<=2;j++)

             {

              printf(" %d  ",j);

             }

             printf("1\n");

          }

              return 0;

          }

      }

       

      c.

      #include<stdio.h>

      int main()

      {

          int i,j,n;

          printf("Enter the number of lines\n");

          scanf("%d",&n);

          for(i=1;i<=n;i++)

          {

              for (j=n;j>=1;j--)

             {

              printf(" %d  ",j);

             }

             printf("\n");

          }

              return 0;

          }

      }

       

       

      d.

      #include<stdio.h>

      int main()

      {

          int i,j,n;

          printf("Enter the number of lines\n");

          scanf("%d",&n);

          for(i=0;i<=n;i++)

       

          {

              for (j=1;j<=n-1;j++)

              printf("");

              for(j=1;j<=i;j++)

              printf(" %d ",j);

              for(j=i-1; j>=1;j--)

       

             printf(" %d ",j);

             printf("\n");

          }

          }

              return 0;

          }

       

      e

      #include<stdio.h>

      int main()

      {

          int i,j,n;

          printf("Enter the number of lines\n");

          scanf("%d",&n);

          for(i=0;i<=n;i++)

       

          {

              for (j=1;j<=n-i;j++)

              printf("  ");

              for(j=1;j<=i;j++)

                  printf(" %d ",j);

                  for(j=i-1;j>=1;j--)

                  printf(" %d ",j);

                  printf("\n");

          }

          }

              return 0;

          }

       

       

      3. Extend the programs of Example5.6 and 5.7 to make it dynamic by accepting the number of lines as an input from the keyboard.

      Solution: 5.6

       

      #include<stdio.h>

      int main()

      {

          int i,j,k,n;

          printf("Enter the number of lines\n");

          scanf("%d",&n);

          for(i=1;i<=n;i++)

       

          {

              for (k=1;k<=n-i;k++)

              printf("  ");

              for(j=1;j<=2*i-1;j++)

                  printf(" X ");

       

                  printf("\n");

          }

          }

              return 0;

          }

       

      Solution: 5.7

       

      #include<stdio.h>

      int main()

      {

          int i,j,k,n;

          printf("Enter the number of lines\n");

          scanf("%d",&n);

          for(i=1;i<=n;i++)

       

          {

              for (k=1;k<=n-i;k++)

              printf("  ");

              for(j=1;j<=2*i-1;j++)

                  printf(" %d ", j);

       

                  printf("\n");

          }

          }

              return 0;

          }

       

       

      4. What is nested loop? Why do one used nested loops in our programs?

      Loop inside loop is called nested loop. We used nested loop for:

      1. It is useful when we are dealing with more number of iteration.

      2. It reduce the program length.

      3. It reduce the memory size of the program.

       

      5. Do we need to use same type of loop as outer and inner loops? Justify with some code segments.

      No, we do not need to use same type of loop as outer and inner loops. If we want to repeat a action continuously till the user want at that time we can use do…while as outer loop and for as inner loop. For example:

      Int flag =0; i;

      do{

      for(i=1,1<=5;i++)

      {

      Statements;

      }

      }

      while (true)


      6. Can we put a third loop inside the inner loop cons tract? Write a c program to justify your answer.

      Yes, we can put a third loop inside the inner loop cons tract. For example, if we want analyze following pattern, we see that we can divide the pattern in two parts as left part and right parts.

                                        X|

                                X    X |   X

                      X     X      X  |   X     X

             X     X      X      X  |   X     X     X

            X    X      X      X     X   |   X     X     X

       

      To display this pattern first we define a inner loop to display the spaces. In second inner loop we will display the left part of the triangle and the third inner loop we will display the right part of the triangle.

       

    Table of Content