C Program to Print Pyramid of Numbers

The following program will print numbers as pyramid. 

The sequence will be  printed as specified in the below triangle format. If user enter number of rows as 5 then following output will be produced. 
             0
          1 0 1
       2 1 0 1 2
    3 2 1 0 1 2 3
 4 3 2 1 0 1 2 3 4

Program to Print Pyramid of Numbers

include

include

int main()
{
int rows, space, i, j;

printf("Enter number of rows: ");// enter a number for generating the pyramid
scanf("%d",&rows);

for(i=0; i<rows; i++)         // outer loop for displaying rows
{
    for(space=1; space <= rows-i; space++)  // space for each and every element
        printf("  ");

    for(j=0-i; j <= i; j++) //inner loop for displaying the pyramid of numbers
    {

        printf("%2d",abs(j));   // prints the value

    }
    printf("\n");         // every line in different row
}

getch();

}

OUTPUT for Pyramid of Numbers

Enter number of rows: 5
0
1 0 1
2 1 0 1 2
3 2 1 0 1 2 3
4 3 2 1 0 1 2 3 4

Leave a Reply

Your email address will not be published.