Inverted Pyramid on C++

how to make the output reverse? I've tried it several times but always failed

so far my code looks something like this

#include <iostream>
using namespace std;

int main()
{
    int rows, number = 1, space;

    cin >> rows;

    for(int i = 1; i <= rows; i++)
    {
        for(space = 1; space <= rows-i; ++space)
        {
            cout <<"  ";
        }

        for(int j = 1; j <= i; ++j)
        {
            cout << number << "   ";
            ++number;
        }

        cout << endl;
    }

    return 0;
}

Output :

      1
    2   3
  4   5   6
7   8   9   10
4

1 Answer

#include <iostream>
using namespace std;

int main()
{
    int rows,space;

    cin >> rows;

    int number=0;
    int x = 1;

    while(x<=rows)
    {
        number=number+x;
        x++;
    }   


    for(int i = 1; i <= rows; i++)
    {
        for(space = 1; space <i; ++space)
        {
            cout <<"  ";
        }

        for(int j = i; j <= rows; ++j)
        {
            cout << number << "   ";
            --number;
        }

        cout << endl;
    }

    return 0;
}

if by inverted you mean this,

10   9   8   7   
  6   5   4   
    3   2   
      1  

then above is the code for that. You need to find out the largest number for given number of rows, in this case it is 10 and then little modifications in your code to meet the requirement.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest