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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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.

You Might Also Like