Home > Back-end >  Multiplication Table Based On BOTH User Inputs
Multiplication Table Based On BOTH User Inputs

Time:12-03

I am trying to create a multiplication table that is based on BOTH user inputs. I want to ask the user the first and second integer and then to print out the first integer by the second integer that amount of times.

For example, if I choose 5 for first integer and 10 for second integer, I want the results printed as such:

5 x 1 = 5
5 x 2 = 10
and so forth...

I do not know whether I should be using for loops or an array for this type of program. I am about 15 weeks of learning C .

When I execute the code, nothing happens in the executable. Here is the code:

cout<<"  Multiplication Table"<<endl;
cout<<"----------------------"<<endl;
cout<<"  Input a number: ";
cin>>multiplyNumber;
cout<<"Print the multiplication table of a number up to: ";
cin>>multiply2Number;
for (int a = multiplyNumber; a < multiply2Number; a  ){
    for (int b = multiply2Number; b < multiply2Number; b  ){
        cout<<a<<" X "<<" = "<<endl;
    }

CodePudding user response:

You don't see the numbers being outputted because your inner for loop is never entered, as b (which has the value of multiply2Number) can never be less than multiply2Number.

Do you want the second number to be the number of entries to display, starting at x 1 and progressing sequentially? If so, then try something like this:

cout << "  Multiplication Table" << endl;
cout << "----------------------" << endl;

cout << "  Input a number: ";
cin >> number;

cout << "Print the multiplication table of a number up to: ";
cin >> lastMultiplier;

for (int multiplier = 1; multiplier <= lastMultiplier;   a){
    cout << number << " X " << multiplier << " = " << (number * multiplier) << endl;
}

Online Demo

Or, do you want the second number to be the highest multiplied value to stop at, and you want to display however many entries is required to reach that value? If so, then try something like this instead:

cout << "  Multiplication Table" << endl;
cout << "----------------------" << endl;

cout << "  Input a number: ";
cin >> number;

cout << "Print the multiplication table of a number up to: ";
cin >> lastResult;

for (int multiplier = 1, result = number; result <= lastResult; result = number *   multiplier){
    cout << number << " X " << multiplier << " = " << result << endl;
}

Online Demo

  • Related