I'm using the Visual Studio C++ and I need to do this output...
____1
___21
__321
_4321
54321
"-" are spaces.
I already got this...
1
12
123
1234
12345
and...
5
54
543
5432
54321
I am only allowed to use the FOR loop and setw.
Last edited on
Well, you use setw...
cout<<setw(5)<<left;
btw either your explanation or your topic name is incorrect :) one is only IF's, other is only FOR's..
xander333's explanation should work :)
I meant FOR.
@xander: Where exactly do I put that? in the code (I left my program in a USB with my friend, I can't post it right now, sorry).
All I remember is
{
int A,B;
{for(A=1, A <=5; A++)
for (B=1, B <=A; B++)
}}
Something like that...sorry about the "IF" thing.
Just add an int w = 5
to your outer loop and decrement it with each iteration.
Excuse my lack of C++ terminology, but can you do an example?
1 2 3 4 5 6 7 8 9
|
for(int i = 1, /*int */w = 5; i <= 5; ++i, --w)
{
std::cout << setw(w);
for(int j = i; j > 0; --j)
{
std::cout << j;
}
std::cout << std::endl;
}
|
I haven't tested or anything but it looks correct to me.
EDIT: there was a mistake in the for loop. I commented it out.
Last edited on
Maybe something like that:
1 2 3 4 5 6 7 8 9 10 11
|
for(int i=1 ; i <= 5 ; i++)
{
for(int j=1 ; j <= (5-i) ; j++)
std::cout<<" ";
for(int j=i ; j >= 1 ; j--)
std::cout<<j;
std::cout<<"\n";
}
|
Last edited on