3 C++ Programs to Generate Multiplication Tables

Generating Multiplication Tables in C++

In this tutorial, we will show you how to generate multiplication tables in C++. A user is asked to enter the number for which he/she wants to generate the table.

Different logics are used to generate the table to 10 and beyond.

C++ Multiplication table – Program 1 using while loop

In this C++ program, we used a while loop to generate the table of an entered number by the user. As you run this program, it will ask you to enter the number that you want to generate the multiplication table for.

After entering the number, it will generate a table till 10. Have a look at the code and a few sample outputs:

#include<iostream>

using namespace std;

int main()
{

    int x;
    int tab_num;

//Taking user input for the number

cout<<"Please enter a number to generate its table: ";

cin>>tab_num;

//Using while loop to iterate till 10

    x = 1;

    while (x <= 10) {

         cout<<tab_num<<" * "<<x<<" = "<<tab_num*x<<"\n";
        x++;
    }

  return 0;

}

Table of 5:

CPP-muti-table-5

Table of 10

CPP-muti-table-10

Table of 25

CPP-muti-table-25

Program 2 – Using for loop to generate the multiplication table

Now, we used a for loop to generate the table. See the code and output:

#include<iostream>

using namespace std;

int main()
{
    int x;
    int tab_num;

//Taking user input for the number

cout<<"Please enter a number to generate its table: ";

cin>>tab_num;

//Using for loop to iterate till 10

    for(int x=1;x<=10;x++)
    {

        cout<<tab_num<<" * "<<x<<" = "<<tab_num*x<<endl;
    }
  return 0;

}

Table of 5:

CPP-muti-table-5

 

Want to go beyond multiplying by 10?

The following example multiplies the entered number for the table to the given number. In the above examples, we multiplied each given number to the range of 10.

In this example, the user is asked to enter the given number for the table. It also asks to specify the range where it should multiply and then it will display the table. Have a look:

#include <iostream>

using namespace std;

int main()

{

    int tab_num, to_range, x;

    //Taking user input both numbers

    cout << "Please enter a number to generate the table: ";

    cin >> tab_num;


    cout << "Please enter the range like 10, 15, 20? ";
    cin >> to_range;

    //Using for loop generate the table

    for (int x = 1; x <= to_range; ++x) {

        cout << tab_num << " * " << x << " = " << tab_num * x << "\n";

    }

    return 0;

}

Table of 5 till 22:

CPP-muti-table-5_22

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!