C++ inverted Pyramid.

Hello everyone, I am new to C++ and I want to make an inverted pyramid so it follows the form of:

54321
x432x
xx3xx

or:

7654321
x65432x
xx543xx
xxx4xxx

The goal is to make it look like that pattern.
The x's denote spaces
Note. this is only for integers under 10.

What is wrong with my code?

#include<iostream>
using namespace std;

int main()
{
int n,i,j;
cout << "Enter a positive odd number: ";
cin >> n;

while(n<0 || n % 2 == 0)
{
cout << "Enter a positive odd interger: ";
cin >> n;
}

for(i=0;i<=0.5*n-0.5;i++)
{
for (j=n;j>=1;j--)
{
cout << j;
}

cout<<endl;
}
return 0;
}

I am not sure how to add the spaces or make it subtract like the pattern. Any help would be great!
Last edited on
Despite of your quasi-unreadable code try this one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int n;
	cout << "Enter a positive odd number: "; // < 10... guess why?
	cin >> n;
	int j = 0;
	for(int k = 1; k < n; ++k)
	{
		cout << setw(k + 5);
		for(int i = j ; i < n - j; ++i)
			cout << n - i ;
		cout << endl;
		j++;
	}
	return 0;
}

And a sample of output:

1
2
3
4
5
6
Enter a positive odd number: 9
     987654321
      8765432
       76543
        654
         5

Happy programming!
Last edited on
I apologize for my awful formatting - I am also new to this forum. I now understand why my program was not working. Is there a way I can achieve this pyramid without including the iomanip command?
<iomanip> is a header of C++ so don't be afraid of it!. Don't forget: we are in the text mode and the text mode of any console has no many facilities. <iomanip> header is specially made to "manipulate" the output of a code and has many built-in functions, so please take a look here:

http://www.cplusplus.com/reference/iomanip/?kw=iomanip

Last edited on
What @condor is telling you is correct but a little advanced for a raw beginner. The purpose of the assignment is to get practice writing loops. The <iomanip> header is included to allow you to use the setw() function which basically allocates k+5 spaces for the first digit you print out, effectively padding the left side of the pyramid with k+4 spaces.

For your assignment, you should skip the setw() statement and replace it with a loop that prints out spaces for you.
Thanks @doug4. I understand how to use the sets() command and understand how it can be applied. Thanks for all the help
@doug4 @condor

How would you do this so it reads values more than 10 and the modulus of 10 is taken? (basically resetting back to zero when n is over 10)

for example, if the user inputted 15, it would read:

543210987654321
x4321098765432X
xx32109876543XX
xxx210987654XXX
xxxx1098765XXXX
xxxxx09876XXXXX
xxxxxx876XXXXXX
xxxxxxx7xxxxxxxx

I'm assuming you would make an if statement in the nested for loop when there is an if statement that reads:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

int main()
{
    int n,i,k;
    int j(0);
    
    cout << "Enter a positive odd number: ";
    cin >> n;
    while(n<0 || n % 2 == 0)
    {
        cout << "Enter a positive odd interger: ";
        cin >> n;
    }
    for(k=1;k<=((n+2)/2);k++)
    {
        cout << setw(k);
        for(i=j;i<n-j;i++)
        {
            if((n-j)>9)
            {
                cout <<abs((n%10)-(i%10));
            }
            else
            {
                cout << n-i;
            }
        }
        cout<< endl;
        j++;
    }
}


This code works fine until you enter in numbers over 29. I don't understand
Last edited on
I found the error! No need to solve anymore
Topic archived. No new replies allowed.