#include <iostream>
#include <iomanip>
using namespace std;
void printPyramid(int final);
int main() {
int final;
cout << "Please enter an integer from 1 to 15: ";
cin >> final;
if (cin.fail()) { // ALWAYS check for errors
cerr << "Invalid input" << endl;
return -1;
}
if (final < 1 || final > 15) {
cerr << "Number must be between 1 and 15" << endl;
return -1;
}
cout << endl; // (not really needed)
printPyramid(final);
}
void printPyramid(int final) {
int width = (final < 10) ? 2 : 3; // num over 10 need column size of 3
for (int row = 1; row <= final; row++) {
// Print spaces.
// There are final - row columns before the first number
for (int col = 1; col <= final - row; col++) {
cout << setw(width) << " ";
}
// Prints numbers decending from row to 1
for (int num = row; num > 0; num--) {
cout << setw(width) << num;
}
// Prints numbers ascending from 2 to row
for (int num = 2; num <= row; num++) {
cout << setw(width) << num;
}
cout << endl;
}
}