Triangle made with numbers from 1 to 9

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 9
Reputation: dr.eu is an unknown quantity at this point 
Solved Threads: 0
dr.eu dr.eu is offline Offline
Newbie Poster

Triangle made with numbers from 1 to 9

 
0
  #1
Dec 4th, 2008
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:
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. int first, last;
  7.  
  8. cout << endl;
  9.  
  10. for (first = 1 ; first <= 9 ; first++)
  11. {
  12. cout << " ";
  13. for (last = 1 ; last <= first; last++)
  14. {
  15. cout <<last;
  16. }
  17. cout<< endl;
  18. }
  19.  
  20. cout << endl << endl << endl;
  21. system("PAUSE");
  22. return 0;
  23. }

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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Triangle made with numbers from 1 to 9

 
0
  #2
Dec 4th, 2008
Simple replace

  1. for (last = 1 ; last <= first; last++)
with
  1. for (last = first ; last >=1; --last)

replace the above line and every thing is fine.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 9
Reputation: dr.eu is an unknown quantity at this point 
Solved Threads: 0
dr.eu dr.eu is offline Offline
Newbie Poster

Re: Triangle made with numbers from 1 to 9

 
0
  #3
Dec 4th, 2008
YES!!!
IT works!
Thank you Laiq Ahmed!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 347 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC