C++ Program to Generate Multiplication Table

In this post, you will learn how to Generate Multiplication Table using C++ programming language.

This lesson will teach you how to create a multiplication table, with a for loop and a mathematical function using the C++ Language. Let’s look at the below source code.

How to Generate Multiplication Table?

RUN CODE SNIPPET
#include <iostream>
using namespace std;
int main() 
{
   int n, i;
   cin>> n;
   cout<< "Enter a number : "<< n <<endl;
   cout<<"\nThe multiplication table for "<< n <<" is as follows:"<< endl;
   for (i = 1; i <= 10; i++)
   cout << n << " * " << i << " = " << n * i << endl;
   return 0;
}

Input

2

Output

Enter a number : 2
The multiplication table for 2 is as follows:
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20

#include <iostream>

  • This line which is called the header file. #include statement tells the compiler to use available files and <iostream>is the name of the specific that we have used in this code. The <iostream> file stands for Input and Output statement.

using namespace std;

  • The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.

int main();

  • This line usually controls the function of the code, as it calls the functions to perform their tasks.
  • The int main()shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.

{ }

  • The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
  • Every statement and value between these braces belong to the main function.

The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.

  1. A Multiplication Table is used to define the multiplication operation of the number. Usually a multiplication table has a range from 1 to 10. The output has the Product of the number with 1 to 10 as seen in the above output.
  2. First we initialize the variables n,i as integers  and we collect the number from the user and store it in the variable n, using function cin>>  and display the value using cout<< and the Insertion Operators'<<‘ , ‘>>’.
  3. Next we use the for loop  and declare the condition (i = 1; i <= 10; i++) , in this condition first the integer  i is initialized as 1, followed by the condition that the value i less than or equal to 10, followed by incremating  the value i.
  4. When the condition is satisfied the value is applied in the loop statement is displayed and the mathematical function n * i  is executed and the answer is displayed.
  5. The for loop is executed multiple times till the condition i <= 10 is not satisfied. The function is completed and the execution is terminated.

Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line and ‘\n’ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.

Additional Source Code

You can also try the source code given below. The following code is structured using a for loop, a mathematical function and an additional statement which asks for a specific range. Let’s look at the below source code.

RUN CODE SNIPPET
#include <iostream>
using namespace std;
int main()
{
    int n, range, i;
    cin >> n;
    cout << "Enter an integer: "<<n<<endl;
    cin >> range;
    cout << "Enter range: "<<range<<endl;
    cout<< "\nThe multiplication table for "<<n<<" is as follows:"<<endl;
    for (int i = 1; i <= range; ++i) 
    {
        cout << n << " * " << i << " = " << n * i << endl;
    }
return 0;
}

Input

3
5

Output

Enter an integer: 3
Enter range: 15
The multiplication table for 3 is as follows:
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
3 * 11 = 33
3 * 12 = 36
3 * 13 = 39
3 * 14 = 42
3 * 15 = 45
  1. This source code has similar functions like the previous code, only with an additional statement which requests the user to enter  a range.
  2. First after initializing the integers, we collect the values from the user and display them using the cin cout functions.
  3. The obtained numbers are used in the for loop. Here instead of the using the default value 10 as the range we use the value entered by the user.
  4. The rest of the functions and statements are similar to Generate the Multiplication Table.