Write a program that generates multiplication tables for the user. The program w
ID: 3641081 • Letter: W
Question
Write a program that generates multiplication tables for the user. The program will ask the user for a number and generate a multiplication table for that number. Do NOT use functions or arrays on this. (You need nested for loops for this!)I am suppose to match this output below when I enter 4.
MULTIPLICATION TABLE: 4's
1 2 3 4
----|----|----|----|
1| 1| 2| 3| 4|
-|----|----|----|----|
2| 2| 4| 6| 8|
-|----|----|----|----|
3| 3| 6| 9| 12|
-|----|----|----|----|
4| 4| 8| 12| 16|
-|----|----|----|----|
heres my code:
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
int main()
{
char Selection;
int num = 0;
int number = 0;
do
{
cout << "MENU ";
cout << "a) Generate Multiplication Table ";
cout << "q) quit program ";
cout << "Please make a Selection: ";
cin >> Selection;
if(Selection == 'a')
{
cout << "Please enter a number to generate a multiplication table ";
cin >> number;
for(int i = 1; i <= number; i++)
{
for (int j = 1; j <= number; j++)
{
cout << fixed << setw(4) << "---" << setw(2) << i*j << " ";
}
}
}
else if(Selection != 'a'|| Selection != 'q')
{
cout << "invalid selection";
}
else if(Selection == 'q')
{
cout<< "quit the program";
}
}while(Selection != 'q');
system("PAUSE");
return 0;
}
Explanation / Answer
This code works with a perfect look:
#include <iomanip.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
char Selection;
int num = 0,i,j;
int number = 0;
clrscr();
do
{
cout << "MENU ";
cout << "a) Generate Multiplication Table ";
cout << "q) quit program ";
cout << "Please make a Selection: ";
cin >> Selection;
if(Selection == 'a')
{
cout << "Please enter a number to generate a multiplication table: ";
cin >> number;
cout<<" ";
for(i = 1; i <= number; i++)
cout<<setw(7)<<i;
cout<<endl<<"--";
for(i = 1; i <= number; i++)
cout<<"------|";
cout<<endl;
for(i = 1; i <= number; i++)
{
cout<<i<<"|";
for (j = 1; j <= number; j++)
{
cout <<setw(4) << "----" << setw(2) << i*j<<"|" ;
}
cout<<endl;
cout<<"--";
for(j = 1; j <= number; j++)
cout<<"------|";
cout<<endl;
}
}
else if(Selection == 'q')
{
cout<< "quit the program";
exit(1);
}
else
cout<<"Invalid Selection. Try again"<<endl;
}while(Selection != 'q');
system("PAUSE");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.