Need help making a times table using the setw

I'm trying to construct a programe that displays the times tables in the form of rows and columns eg:

1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 16 18 20 22
3
4
5
6
7
8
9
10
11
12


you get the picture, the problem is I cant figure out how to get this to work for me, below is my code, it does the math, but how to display it in the way I want escapes me...any feed back would be much appreciated.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    for (int x =1; x<=12; ++x)
    {
        for (int y =1; y<=12; ++y)
        {
            // this is causing me alot of grief >.<
            //cout << setw (5);
            cout << (y*x) << " " << endl;
        }
    }



    cout << "\n\aIt works YAY!" << endl;
    return 0;
}
The problem is that you have an end line after every number, so each has its own line. You should only have an end line after a row is computed. Also, the blank space has no purpose, because setw(5) adds needed space.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    for (int x =1; x<=12; ++x)
    {
        for (int y =1; y<=12; ++y) cout << setw(5) << y*x;
        cout << endl;
    }
    cout << "\n\aIt works YAY!" << endl;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.