Thursday, February 5, 2009

Write a C program to construct a pyramid of numbers.

/*
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
*/

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
int r, c, num=1 ,len;
clrscr( );
printf("Enter the required no. of rows:");
scanf("%d", &len);
for( r=1; r< =len; r++ ) {
printf("\n");
for(c=1 ; c<=r; c++) {
printf("%2d", num);
num++; }
}
getch( );
return 0;
}
Explanation:-
Here 'r' indicates row,
'c' indicates column and 'len' indicates length of the pyramid.
And lastly 'num=1' for generating pyramid of numbers.
This 'c<=r' is required becoz when row is incremented, column value is also incremented.
Note:- If U have any doubts regarding this program, contact with me through my email-id.

No comments:

Post a Comment