Write a C program to print the given pyramid of numbers.
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
int main() {
int n, i, j, k, val;
printf("Enter the number of lines:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
/* loop for space at the front half of triangle */
for (j = i; j < n; j++) {
printf(" ");
}
val = i;
/* loop to print values at the front half of triangle */
for (k = 1; k <= i; k++) {
printf("%3d", val++);
}
val = val - 2;
/* loop to print values at the second half of triangle */
for (k = 1; k < i; k++) {
printf("%3d", val--);
}
printf("\n");
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the number of lines:5
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
Enter the number of lines:5
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
No comments:
Post a Comment