looping

how to make a triangle using the asterisk(*).
help e!!!
I'm guessing you are asking for help in printing out a triangle like this:
1
2
3
4
5
6
*
**
***
****
*****
******


using a 'for' loop. But whether you meant that shape or not, this should be posted in the General C++ section, rather than the lounge. The lounge is for off-topic topics. :D I don't know if you can move this or not, I've never tried before. But if you can get it there, I'm sure someone would be glad to help.

Also, try to use full sentences, proper grammar, and correct spelling to the best of your ability (if english isn't your first language, mistakes are understandable). That will certainly improve the amount people are willing to help you, because it will appear that you are grateful for their time and efforts.

Sorry for that 'lecture.'

Thanks!
Numeri
closed account (EwCjE3v7)
You can move it...just press edit on top and press general c++ programming at bottom and save
Oh! I remember my first time doing that! I had the one like Numeri then I had to change it, I used space character and deducted the number then adding the *

eg

25 spaces *
23 spaces ***

Till it filed up to the bottom. ☺ hope that helps

though there is a mathematical way, It was my simplest way while learning Accelerate C++
Last edited on
hey
I did that before and it requires a nested for loop. The outter main loop is the lines the triangle takes up. The second loop contains the asterisks(*) and the inner look contains spaces (' ') .

example for the triangle above:

for (int i = 5; i>0; i--)
{
for(int j = 0; j< i-1; j++)
cout<<' ';
for(int k = i; k<= 5; k++)
cout<<'*';
cout<<endl;
}
}
closed account (Dy7SLyTq)
it doesnt require a nested loop. i could write one without it
It's probably easiest to do that way though, although a recursive solution may be even more elegant (at the cost of performance due to function call overhead and the risk of a stack overflow if you try to recurse too many times).
Topic archived. No new replies allowed.