Look, making the program to cout that with a formula using for loops would be much harder and use much more code than simply printing the numbers in order by themselves.
printf("\t\t\t 1\n\t\t\t 121\n\t\t\t 12321\n\t\t\t 1234321\n\t\t\t123454321\n");
is a good option, but if you want to use spaces instead of \t or one printf each line then it's your decision :)
You have to use logical thinking here. Mainly learn how to do it step by step. First, start with drawing the numbers using a for loop:
1
12
123
1234
12345
Then a for loop for drawing the back end
1
121
12321
1234321
123454321
Then a for loop for drawing the spaces
1
121
12321
1234321
123454321
You come up with this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <cstdio>
int main() {
for (int i = 1; i <= 5; i ++) {
// Draw spaces
for (int j = 5; j > i; j --)
printf(" ");
// Display first set of numbers
for (int j = 1; j < i; j ++)
printf("%i", j);
// Display last set of numbers
for (int j = i; j >= 1; j --)
printf("%i", j);
// End the line
printf("\n");
}
return 0;
}
int rows = 7; //number of rows you want to print
int mid = 2 * (rows - 1) + 1 / 2; //determine the midpoint of the pyramid
for (int i = 0; i < rows; i++) {
int midValue = i + 1; //the middle value for the row
int j = 1;
printf("%*c", mid); //pad proper number of spaces
for (; j <= midValue; j++) { //print upto and including midvalue
printf("%d", j);
}
j -= 2; //need to subtract 2 because j is now 2 more than the mid value
for (; j >= 1; j--) { //print from 1 less than mid value to 1
printf("%d", j);
}
printf("\n");
mid--;
}