Write a C++ program which will ask the user to enter an integer from 1 to 15 and displays a number pyramid (implement input validation). For example, if the input is 4 the output will be as follows:
* * *1
* * 2 1 2
* 3 2 1 2 3
4 3 2 1 2 3 4
My professor said we must use nested loops.
This is what i have so far the 2nd inner loop cause a half pyramid. The 1st inner loop I am trying to make the opposite of the 2nd inner loop. I am unsure you can logically do that however.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int n , i , j , k;
do
{
cout << "Enter an integer between:" << endl;
cin >> n;
cout << endl;
}
while ( n < 0 || n > 16 );
for ( i = 0 ; i < n ; i++ )
{
for( j = 1 ; j = 1 ; i++)
{
cout << right << setw(3) << j;
}
for ( k = 1 ; k <= i + 1 ; k++ )
{
cout << left << setw(3) << k;
}
cout << endl;
}
return 0;
}