Posted: Sun Feb 24, 2008 10:23 pm Post subject: Loop (continued)
I'm having a little difficulty on this one:
Use nested loops that print the following pattern.
code:
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Here's what I did:
code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
for (int i = 6; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
cout << setw(3) << j;
}
cout << endl;
}
return 0;
}
The result was:
code:
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
I want to get the 2nd through the 6th row kinda moved to right, to make it look like the one above. I'm having a difficulty rewriting the code. Can anyone help, please?
Thanks,
J.
Sponsor Sponsor
OneOffDriveByPoster
Posted: Mon Feb 25, 2008 1:06 am Post subject: Re: Loop (continued)
You can still print spaces to get want...
md
Posted: Tue Feb 26, 2008 11:09 am Post subject: RE:Loop (continued)
use setw perhaps?
#include <iomanip>
and then cout << setw(12) << n;
The easiest way is probably to use string streams. The fastest and best way is probably to output spaces, how many spaces depends on the line.