Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiplication Table Based On BOTH User Inputs

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++.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
    }

>Solution :

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 >> multiplyNumber;

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

for (int a = 1; a <= multiply2Number; ++a){
    cout << multiplyNumber << " X " << a << " = " << (multiplyNumber * a) << 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 >> multiplyNumber;

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

for (int a = 1, b = multiplyNumber; b <= multiply2Number; b = multiplyNumber * ++a){
    cout << multiplyNumber << " X " << a << " = " << b << endl;
}

Online Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading