Create a program that makes a multiplication table. Ask the user how many number
ID: 670754 • Letter: C
Question
Create a program that makes a multiplication table. Ask the user how many numbers should be in the table.
Requirements
Store all of the data in a 2-dimensional vector of ints.
Allow the program to run repeatedly until the user is finished.
Do not allow inputs outside the range of 1-9
Sample Run
Enter the numbers for multiplication table (1-9): -1
Enter the numbers for multiplication table (1-9): 10
Enter the numbers for multiplication table (1-9): 4
| 1 2 3 4
- | - - - -
1 | 1 2 3 4
2 | 2 4 6 8
3 | 3 6 9 12
4 | 4 8 12 16
| 4 3 2 1
- | - - - -
4 | 16 12 8 4
3 | 12 9 6 3
2 | 8 6 4 2
1 | 4 3 2 1
Do you want to do another? (y/n) y
Enter the numbers for multiplication table (1-9): 3
| 1 2 3
- | - - -
1 | 1 2 3
2 | 2 4 6
3 | 3 6 9
| 3 2 1
- | - - -
3 | 9 6 3
2 | 6 4 2
1 | 3 2 1
Do you want to do another? (y/n) n
Things to Consider
You can use setw(n) before any number and it will add blank spaces to show the number in n columns. That is how to line up your columns.
Don't forget you have 0 based indexes and numbers that go from 1 to 9.
Explanation / Answer
#include<iostream.h>
void main()
{
int n,i,j,a[9][9];
char ans='y';
while(ans=='y')
{
cout<<"enter number for multiplication table [1-9]";
cin>>n;
if(n>1 && n<=9)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=(i+1)*(j+1);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<" ";
}
}
cout<<"Do you want to continue [y/n]";
cin>>ans;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.