In this tutorial, we will write a C++ program to generate a multiplication table for any number and also for the given range.
In order to understand the program, you should go through the following topic in C++.
C++ Program to Print Multiplication Table of any Given Number
The program will print the table of the number entered by the user up to 10. The program uses for loop to iterate and print the number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> using namespace std; int main() { int n; cout << "Enter the number: "; cin >> n; for (int i = 1; i <= 10; ++i) cout << n << " * " << i << " = " << n*i << endl; return 0; } |
Output:
Enter the number: 4
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
C++ Program to Print Multiplication Table of any Number upto a given range
This program will work as same as the above, the extra feature added here is the range. The above program displays the table of any number up to 10 but here it will print the table up to the user’s choice.
The program takes user input for the number whose table needs to be displayed and also the range up to which number it should display.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> using namespace std; int main() { int num, range; cout << "Enter the number: "; cin >> num; cout << "Enter range: "; cin >> range; for (int i = 1; i <= range; ++i) cout << num << " * " << i << " = " << num*i << endl; return 0; } |
Output:
Enter the number: 6
Enter range: 15
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60
6 * 11 = 66
6 * 12 = 72
6 * 13 = 78
6 * 14 = 84
6 * 15 = 90