Help on Multiplication table

Here is the problem. Whenever i input something less then 10 it works fine except i get the press any key thing 2 times, any input above 10 i get weird stuff. Also if n is 0 or above default value (32) i need to prompt the user enter a valid value or use the default value 10. Another question is that on output i want it to look like something like this
1 2 3 4
-------
1|1 2 3 4
2|2 4 6 8
3|3 6 9 12
4|4 8 12 16
i have got the column part but i don't know how to do row part.Any help will be appreciated.Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <iomanip>
using namespace std;

int main() 
{	
	int n;
	/*variables and declaration of the array*/
	const int row = 32;
	const int colm = 32;
	int mt[row][colm]= {0} ;
	/*loop for multiplication*/
	cout << "Please enter a number. " << endl;
	cin >> n;
		for(int i=0;i <= n; i++)
	{
	 for(int j=0;j <= n; j++)
	 {
		 mt[i][j] = i * j;
		 
	 }
	}
	
	/*loop for displaying multiplication table*/
	cout << "-------------------------------------------------------" << endl;
	for (int i = 1; i <= n; i++)
	{
		cout << i  << " |  "<< "\t";
	
		for (int j=1; j <= n; j++)
			
			cout << mt[i][j] << "\t";
			cout << endl;
		
		}

	system("pause");
}

Please enter a number.
9
-------------------------------------------------------
1 |     1       2       3       4       5       6       7       8       9

2 |     2       4       6       8       10      12      14      16      18

3 |     3       6       9       12      15      18      21      24      27

4 |     4       8       12      16      20      24      28      32      36

5 |     5       10      15      20      25      30      35      40      45

6 |     6       12      18      24      30      36      42      48      54

7 |     7       14      21      28      35      42      49      56      63

8 |     8       16      24      32      40      48      56      64      72

9 |     9       18      27      36      45      54      63      72      81

Press any key to continue . . .
Press any key to continue . . .
Please enter a number.
16
-------------------------------------------------------
1 |     1       2       3       4       5       6       7       8       9
10      11      12      13      14      15      16
2 |     2       4       6       8       10      12      14      16      18
20      22      24      26      28      30      32
3 |     3       6       9       12      15      18      21      24      27
30      33      36      39      42      45      48
4 |     4       8       12      16      20      24      28      32      36
40      44      48      52      56      60      64
5 |     5       10      15      20      25      30      35      40      45
50      55      60      65      70      75      80
6 |     6       12      18      24      30      36      42      48      54
60      66      72      78      84      90      96
7 |     7       14      21      28      35      42      49      56      63
70      77      84      91      98      105     112
8 |     8       16      24      32      40      48      56      64      72
80      88      96      104     112     120     128
9 |     9       18      27      36      45      54      63      72      81
90      99      108     117     126     135     144
10 |    10      20      30      40      50      60      70      80      90
100     110     120     130     140     150     160
11 |    11      22      33      44      55      66      77      88      99
110     121     132     143     154     165     176
12 |    12      24      36      48      60      72      84      96      108
120     132     144     156     168     180     192
13 |    13      26      39      52      65      78      91      104     117
130     143     156     169     182     195     208
14 |    14      28      42      56      70      84      98      112     126
140     154     168     182     196     210     224
15 |    15      30      45      60      75      90      105     120     135
150     165     180     195     210     225     240
16 |    16      32      48      64      80      96      112     128     144
160     176     192     208     224     240     256
Press any key to continue . . .
I don't see anything weird in the display, except that after the digit 9, the lines wrap to the next line because the width of the screen is usually only 80 characters wide. You'll see the first two rows have digits 1 to 16, as by the number inputted. The second two rows are 1 through 16 multiplied by two, and the next two lines, by three's, and so on. As to the n, you could make a do/while loop. After the int n;, put n=0;. Then do{ .. your input request }while (n<=0);. Now, if a 0 is requested, it will just loop back with a "Please enter..." request again.
Hope this helps..
Last edited on
Topic archived. No new replies allowed.