Write a C program to print triangle numbers pattern using for loop.
#include <stdio.h> int main() { int rows; printf("Enter Triangle Number Pattern Rows = "); scanf("%d", &rows); printf("Printing Triangle 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 ", k); } printf("\n"); } }
This example prints the triangle pattern of numbers using a while loop.
#include <stdio.h> int main() { int i, j, k, rows; printf("Enter Triangle 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 ", k); k++; } printf("\n"); i++; } }
Enter Triangle Number Pattern Rows = 9
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
Write a program to print triangle numbers pattern using a do while loop.
#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 ", k); } while (++k <= i); printf("\n"); } while (++i <= rows); }
Enter Rows = 13
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12 13