0

Hi!

I have to write program, who print Triangle made with numbers from 1 to 9,like this:

1
21
321
4321
54321
654321
7654321
87654321
987654321

I wrote code:

#include <iostream.h>
#include <stdlib.h>

int main()
{
 int first, last;

 cout << endl;

 for (first = 1 ; first <= 9 ; first++)
 {
    cout << " ";
    for (last = 1 ; last <= first; last++)
    {
       cout <<last;
    }
 cout<< endl;
 }

 cout << endl << endl << endl;
 system("PAUSE");
 return 0;
}

which put on the screen Triangle in form:
1
12
123
1234
12345
123456
1234567
12345678
123456789

I think, that I need to use if statment to invert numbers, but i dont know how. Any ideas?

2
Contributors
2
Replies
4
Views
8 Years
Discussion Span
Last Post by dr.eu
0

Simple replace

for (last = 1 ; last <= first; last++)

with

for (last = first ; last >=1; --last)

replace the above line and every thing is fine.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.