Write a C program to print a pyramid numbers pattern using nested for loop or a program to print a pyramid pattern of numbers.
#include <stdio.h> int main() { int rows; printf("Enter Pyramid Number Pattern Rows = "); scanf("%d", &rows); printf("Printing Pyramid Number Pattern\n"); for (int i = 1; i <= rows; i++) { for (int j = rows; j > i; j--) { printf(" "); } for (int k = 1; k <= i; k++) { printf("%d ", i); } printf("\n"); } }
This C program helps to print a pyramid pattern of numbers using a while loop.
#include <stdio.h> int main() { int i, j, k, rows; printf("Enter Pyramid Number Pattern Rows = "); scanf("%d", &rows); printf("\n"); i = 1; while (i <= rows) { j = rows; while (j > i) { printf(" "); j--; } k = 1; while (k <= i) { printf("%d ", i); k++; } printf("\n"); i++; } }
This example uses the do while loop to print the pyramid pattern of numbers.
#include <stdio.h> int main() { int i, j, k, rows; printf("Enter Rows = "); scanf("%d", &rows); printf("\n"); i = 1; do { j = rows; do { printf(" "); } while (j-- > i); k = 1; do { printf("%d ", i); } while (++k <= i); printf("\n"); } while (++i <= rows); }
Enter Rows = 9
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9