- #1
Marioqwe
- 68
- 4
Multiplication Table -- nested for loop
Use nested loops to print a M by N multiplication table as following. Allow the user to enter the size of the table (M by N). 1 2 3 4 5 6 ... M
1 1 2 3 4 5 6 ... M
2 2 4 6 8 10 ... 2M
...
N N 2N ....... NM
I don't know how to get the header to move to the right so that it is aligned with the other columns. Could anybody give me any hints?
Thank You!NEVERMIND.
My head enlightened and I figured it out :)
Homework Statement
Use nested loops to print a M by N multiplication table as following. Allow the user to enter the size of the table (M by N). 1 2 3 4 5 6 ... M
1 1 2 3 4 5 6 ... M
2 2 4 6 8 10 ... 2M
...
N N 2N ....... NM
Homework Equations
The Attempt at a Solution
Code:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int w, l;
cout << "Please enter width 'w'" <<endl;
cin >> w;
cout << "Please enter length 'l'" << endl;
cin >> l;
// TOP HEADER
cout << " ";
int k;
for( k=1; k<=w; k++)
{
cout << setw(4) << k << " |";
}
cout << endl;
// NEST LOOP FOR MULT TABLE
int i, j, result;
for( i=1; i<=l; i++ )
{
cout << setw(4) << i << " :";
for( j=1; j<=w; j++ )
{
result = i*j;
cout << setw(4) << result << " |";
}
cout << endl;
}
system("pause");
return (0);
}
I don't know how to get the header to move to the right so that it is aligned with the other columns. Could anybody give me any hints?
Thank You!NEVERMIND.
My head enlightened and I figured it out :)