Thursday, July 30, 2009

How do I print triangle using asterisk in C++?

How do I print triangle using asterisk in C++








Example: I entered a number 4


then the output will print like this…














aaaaaa *


aaaaaa * *


aaaaaa* * *


aaaaa * * * *








remember: the output will depend in the number you input…





(a) is the space, only the asterisk will be print...

How do I print triangle using asterisk in C++?
by using a little intelligence.





hints:





1) for printing stars for each line - a for loop with a single cout%26lt;%26lt;'*' which runs as many times as the line no. ( once for the first line and 4 times for the fourth line)





2) for printing spaces -the opposite of the above scheme: a for with cout %26lt;%26lt; 'a' ( a for space) and running as many times as your input + a fixed number of times for the first line, and "1+ the same fixed number" times for the last line.





the problem if you do this verbatim is you'll probably get a right angled triangle..to solve that you have use some trick (like printing two stars more than the above line for each line).





hope this helped!


No comments:

Post a Comment