Thread: Struck at number pyramid !!

  1. #1
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79

    Struck at number pyramid !!

    Well, i don't know how to print this output using loops ..i tried my best but i am not getting it..
    I want to print this pattern -


    -----------11
    --------11 10 11
    ------11 10 9 10 11
    ----11 10 9 8 9 10 11

    ( '---' are not in the output..i have used '---' only to show the exact pattern here)

    Please help !!
    Last edited by rajarshi; 11-04-2011 at 12:03 AM.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Sit sown and think about the problem. Then write out, in words, how to solve it. Once you can describe in words what you want your program to do, you will be able to translate those steps into code.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    sir, I spend 2 days in doing so...I am not getting any clue
    please suggest

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Okay, a couple of hints. Think about the following questions

    How many rows will be in the completed pyramid?
    If we are currently printing out, say, the 5th row how many numbers will need to be printed out?
    What should be the 8th number of the 6th row, and how did you work that out?


    The essential logic of the program will go something like this
    Code:
    /* work out how many rows need to be printed */
    for( row = 1; row<= how_many_rows; row++ ) {
       /* work out how many numbers need to be printed for this row */
       for( n = 1; n <= how_many_numbers_in_this_row; n++ ) {
          /* work out which number needs to be printed, and print it */
       }
    }
    The commented bits correspond directly to the questions I asked above.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Thanks a lot sir...let me try it !

  6. #6
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by TheBigH View Post
    Okay, a couple of hints. Think about the following questions

    How many rows will be in the completed pyramid?
    If we are currently printing out, say, the 5th row how many numbers will need to be printed out?
    What should be the 8th number of the 6th row, and how did you work that out?


    The essential logic of the program will go something like this
    Code:
    /* work out how many rows need to be printed */
    for( row = 1; row<= how_many_rows; row++ ) {
       /* work out how many numbers need to be printed for this row */
       for( n = 1; n <= how_many_numbers_in_this_row; n++ ) {
          /* work out which number needs to be printed, and print it */
       }
    }
    The commented bits correspond directly to the questions I asked above.


    Thanks a lot sir, I got the concept !!

    Code:
    
    
    Code:
    #include <stdio.h>
    int main()
    {  
     int row,n,j,x,m,k;
    
    
    /* work out how many rows need to be printed */
    for( row = 1; row<= 4; row++ ) 
    { 
        for (k=1;k<=8-row;k++) 
        printf ("    ");
    
    
     
          /* work out which number needs to be printed, and print it */
          for (j=11 ; j>=12-row;j--)
              {
              printf (" %d ",j);
              x=j;
              }
          
          
       
       
            for (m=x+1 ; m<=11;m++)
              printf (" %d ",m);
       
                 
       printf ("\n");
    }
        
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Symmetrical Number Pyramid Help Please
    By HunterTnTU in forum C++ Programming
    Replies: 5
    Last Post: 10-12-2011, 10:15 AM
  2. pyramid char..
    By Kinshara in forum C Programming
    Replies: 3
    Last Post: 11-05-2009, 12:23 PM
  3. [ask]pyramid number with loop
    By nitediver in forum C Programming
    Replies: 2
    Last Post: 10-13-2009, 07:57 AM
  4. Number pyramid
    By Tehy in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 09:01 AM
  5. Making a pyramid of Xs
    By Tride in forum C++ Programming
    Replies: 8
    Last Post: 12-12-2003, 05:14 PM