943,957 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 569
  • C++ RSS
Dec 4th, 2008
0

Triangle made with numbers from 1 to 9

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dr.eu is offline Offline
9 posts
since Dec 2008
Dec 4th, 2008
0

Re: Triangle made with numbers from 1 to 9

Simple replace

cpp Syntax (Toggle Plain Text)
  1. for (last = 1 ; last <= first; last++)
with
cpp Syntax (Toggle Plain Text)
  1. for (last = first ; last >=1; --last)

replace the above line and every thing is fine.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Dec 4th, 2008
0

Re: Triangle made with numbers from 1 to 9

YES!!!
IT works!
Thank you Laiq Ahmed!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dr.eu is offline Offline
9 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reading the first character of a line
Next Thread in C++ Forum Timeline: what is the execution sequence of this code?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC