Number Pyramid
Completed
Read n and print a centred pyramid: row i has n−i spaces, then the numbers 1..i..1 with no spaces between.
Input
One integer (1–9).
Output
n rows.
Example
Input
4
Output
1 121 12321 1234321
Editor
C
Hints
Three inner loops: spaces, counting up, counting down.
Solution explanation
Breaking a row into parts makes pattern problems manageable.
Show solution
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int s = 0; s < n - i; s++) printf(" ");
for (int j = 1; j <= i; j++) printf("%d", j);
for (int j = i - 1; j >= 1; j--) printf("%d", j);
printf("\n");
}
return 0;
}