In this tutorial, we will learn and write a program in C to display the diagonal number pattern. So you may go through the following topic in C.
Diagonal Number Pattern in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <stdio.h> int main() { int i, j, rows; printf("Enter the no. of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; i++) { for (j = i; j < rows; j++) printf(" "); printf(" %d\n", i); } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <stdio.h> int main() { int i, j, k = 1, rows; printf("Enter the no. of rows: "); scanf("%d", &rows); for (i = 1; i <= (2 *rows) - 1; i++) { for (j = 1; j <= (2 *rows) - 1; j++) { if (i == j || i + j == 2 *rows) printf(" %d", k); else printf(" "); } if (i < rows) k++; else k--; printf("\n"); } return 0; } |
Output: