C++ Program: Write a program that reads in the size of a multiplication table an
ID: 3826774 • Letter: C
Question
C++ Program: Write a program that reads in the size of a multiplication table and then prints out hte table. Your program should work for tables of all sizes between 1 and 15. When the user enters the table size, make sure it is a number from 1 to 15. If it is not a valid size, ask the user to re-enter the number. Allow the user to construct as many nultiplication tablesas they would like before ending the program.
Example output: Enter the table size: 25 Size must be between 1 and 20 Enter the table size 5 3 12 15 4 2 16 20 10 15 20 25 Would you like to print another table (Y or N) Enter the table size: 1 Would you like to print another table (Y or N) Enter the table size 3 2 4 6 3 3 9 Would you like to print another table (Y or N) N End program.Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int n;
char ch = 'y';
while(ch == 'y' ||ch=='Y'){
cout << "Enter the table size: ";
cin >> n;
if(n >= 1 && n <=15){
cout<<" ";
for(int i=1; i<=n; i++){
cout<<i<<" ";
}
cout<<endl;
for(int i=1; i<=n; i++){
cout<<i<<" ";
for(int j=1; j<=n ; j++){
cout<<i*j<<" ";
}
cout<<endl;
}
cout<<"Would you like to print another table (Y or N): ";
cin >> ch;
}
else{
cout<<"Size must be between 1 and 15, ";
}
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the table size: 5
1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25
Would you like to print another table (Y or N): y
Enter the table size: 25
Size must be between 1 and 15, Enter the table size: 3
1 2 3
1 1 2 3
2 2 4 6
3 3 6 9
Would you like to print another table (Y or N): y
Enter the table size: 1
1
1 1
Would you like to print another table (Y or N):
n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.