Originally Posted by
TheBigH
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:
#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");
}
}