# Symmetrical Number Pyramid Help Please

• 10-11-2011
HunterTnTU
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; }```
• 10-11-2011
rmatze
I would suggest working it out in steps.

1. figure out how you would print "* * * 1" if 4 is entered.
2. Next figure out how you would print the next line, " * * 2 1 2".

After that you should be able to use the same algorithm to print the rest.
• 10-11-2011
HunterTnTU
I forgot to say that the "*"s are not part of the out put they are just to keep the spaces for some reason the spaces went away and this happened:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
Sorry for that confusion I should of clarified that first.
• 10-11-2011
rmatze
No problem, you would go about it the same way. Also, I noticed your while loop is not doing what you think it's doing, you need brackets.
• 10-11-2011
HunterTnTU
Update * Close but no cigar*
So, I have tweaked it some and now I have it giving the out put
1
212
23123
2341234

I am having a major pain trying to figure out how to make the 2nd inner loop reverse the number order and make the out put take on the form of a triangle. Maybe its because of frustration.

Code:

```#include <iostream> #include <iomanip> using namespace std; int main () {     int n , i , j , k;             cout << "Enter an integer between 1 and 15:" << endl;             cin >> n;         cout << endl;         while ( n < 0 || n > 16 )     {         cout << "Invalid input! Input must be between";     }         for ( i = 0 ; i < n ; i++ )     {         for( j = 2 ; j <= i + 1 ; j++)         {             cout << setw(3) << j;         }             for ( k = 1 ; k <= i + 1 ; k++ )             {                 cout << setw(3) << k;             }                 cout << endl;     }     return 0; }```
• 10-12-2011
rmatze
Instead of counting up your j for loop, count down. It should print

1
21
321
4321