user2840960
user2840960

Reputation: 49

C++ Inverse number pyramid

Okay so i'm trying to make a code that will read in a positive odd integer and output an inverse pyramid starting with that number and descending to 1 and cutting off the first and last digit in the next line and so on. So if i entered 7 it would display:

7654321
 65432
  543
   4

The i 'th row contains n-(2i-2) but I'm not sure how to use that.

Thanks for your help.

This is what I have so far:

#include <iostream>
using namespace std;

int main()
{
    int n,i,j;

    cout << "Enter a positive odd number: " << endl;
    cin >> n ;

    i=n;
    while(n%2 ==0)
    {
        cout<< "Invalid number." << endl;
        cout<< "Enter a positive odd number: " << endl;
        cin >> n ;
    }

    for(i=n; i<=n && i>0 ; i--)
    {
        for(j=i; j<=i; j--)
        {
            cout<< i%10 ;
        }
        cout<<endl;
    }

    return(0);
}

Upvotes: 1

Views: 2286

Answers (1)

MSalters
MSalters

Reputation: 179779

Number the character positions on screen like this:

+----+----+----+----+----+----+----+
| 0 0| 0 1| 0 2| 0 3| 0 4| 0 5| 0 6|
+----+----+----+----+----+----+----+
| 1 0| 1 1| 1 2| 1 3| 1 4| 1 5| 1 6|
+----+----+----+----+----+----+----+
| 2 0| 2 1| 2 2| 2 3| 2 4| 2 5| 2 6|
+----+----+----+----+----+----+----+
| 3 0| 3 1| 3 2| 3 3| 3 4| 3 5| 3 6|
+----+----+----+----+----+----+----+

and check what goes in there

+----+----+----+----+----+----+----+
|  7 |  6 |  5 |  4 |  3 |  2 |  1 |
+----+----+----+----+----+----+----+
|    |  6 |  5 |  4 |  3 |  2 |    |
+----+----+----+----+----+----+----+
|    |    |  5 |  4 |  3 |    |    |
+----+----+----+----+----+----+----+
|    |    |    |  4 |    |    |    |
+----+----+----+----+----+----+----+

Now find the relation between x, y, the value to print, and the initial number.

Upvotes: 5

Related Questions