Write a nested loop to create following pattern

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tienzyee
    New Member
    • Oct 2006
    • 6

    Write a nested loop to create following pattern

    I have to write a nested loop using for statement to produce the following pattern if the user input is 5

    Please enter an integer > 5 (this will be using printf and scanf to get the integer)

    1
    121
    12321
    1234321
    123454321


    and also another program (different coding) to produce the following pattern if user input is 5:


    1
    2 2
    3 3
    4 4
    55555555
  • MitchMitchell
    New Member
    • Oct 2006
    • 2

    #2
    Originally posted by tienzyee
    I have to write a nested loop using for statement to produce the following pattern if the user input is 5

    Please enter an integer > 5 (this will be using printf and scanf to get the integer)

    1
    121
    12321
    1234321
    123454321


    and also another program (different coding) to produce the following pattern if user input is 5:


    1
    2 2
    3 3
    4 4
    55555555
    Do you need help with printing or the loops?

    The first loop is a simple for loop (i=1;i<=input;i ++)
    {j=input-1;
    print i;
    if i=input
    { while(i>0)
    if(j>0)
    {print j;
    --j;}
    insert end of line;
    }

    You get the idea. Next one is just a simple variation.

    Mitch

    Comment

    • tawanda
      New Member
      • Oct 2006
      • 3

      #3
      Hie, for the first program you can use this code:
      int i,j;
      for(i=1;i<=5;i+ +)
      {
      {
      for(j=1;j<=i;j+ +)
      printf("%d",j);
      for(j=(i-1);j>0;j--)
      printf("%d",j);
      }
      printf("\n");
      }

      Comment

      • tienzyee
        New Member
        • Oct 2006
        • 6

        #4
        I'm so sorry, actually i mean the looping, not print the numbers, and also the message earlier doesnt allow me to type space in between....i want the following output using while loop statement....pl ease help...thank you

        ######1######
        #####2#2#####
        ####3###3####
        ###4#####4###
        ##555555555##

        in which # symbol is actually space....and 5 is the input


        and also this output (for another different program), input also 5

        ######1######
        #####121#####
        ####12321####
        ###1234321###
        ##123454321##

        Comment

        • D_C
          Contributor
          • Jun 2006
          • 293

          #5
          I'm not sure what is supposed to happen for numbers greater than nine. This does the simpler case (with spaces in between).
          Code:
          int i = 1;
          int count = 0;
          while(i <= input)
          {
            while(count < ((2*input)+3))
            {
              if( ... )
                print a space
              else if( ... )
                     print count
                   else if ( ... )
                     print either input or count
                        else
                          print a space
          
              count++;
            }
            i++;
          }

          Comment

          • tienzyee
            New Member
            • Oct 2006
            • 6

            #6
            i'm so sorry but i'm just starting to learn c in which i dun really understand the code that you gave, can someone make it simpler?

            Comment

            • micangello
              New Member
              • Oct 2006
              • 3

              #7
              /* ###1### in this case input is 4 when i=0 u have 3 spaces
              ** ##2#2## when i=1 u have 2 spaces
              ** #3###3# when i=2 u have 1 space
              ** 4#####4 when i=3 u have 0 space
              */








              #include<stdio. h>
              main()
              {
              int i, j, numspace, input;
              printf(" Type a number ");
              scanf("%d", &input);
              i = 0;


              while(i < input)
              {
              numspace = input - ( i + 1); // in the first line when input is 4 and i is 0
              // u have 4 - (1 + 0 ) spaces
              // now a loop for printing spaces
              for( j = 0; j< numspace; j++)
              printf(" "); // this prints spaces
              // now to print the number... after printing the spaces u simply print the number
              // whis is ( i + 1 ) since on the first line.. the number is 1 and i is 0
              // on the 2nd line where i is one.. the number is 2 .. that is ( i + 1 ) and so on
              // so we have the following
              printf("%d", (i + 1) );
              // now.. to print the other number on the line...
              // note that on the second line u have this sequence 2#2 one space btween them where i = 1
              // and on the 3rd line........... ............... ... 3###3 3spaces between them where i = 2
              // soo.. we have 2*i -1 spaces beween the number
              // for i = 1 ... 2*1 - 1 = 1 for i = 2.... 2*2 - 1 = 3
              // so.. we loop again for the other spaces
              // but first we define the number of spaces
              if(i>0) // since on the first line u only have one number
              {
              numspace = 2*i -1;
              for(j=0; j<numspace; j++)
              printf(" ");
              printf("%d", i+1);
              }


              // almost done.. now we have to print a newline and increment i
              printf("\n");
              i++;

              }
              }

              Comment

              • tienzyee
                New Member
                • Oct 2006
                • 6

                #8
                yeah that works but what about 5 ?the last line must print all 555555....

                what about coding this program with only for statements? is it possible?

                Comment

                • tienzyee
                  New Member
                  • Oct 2006
                  • 6

                  #9
                  erm...can someone answer me please...

                  Comment

                  Working...