C++ Program to Generate Multiplication Table
In this example, we learn to generate the multiplication table of a number (entered by the user) using for loop, while loop and do-while loop and also with range.
To understand this example, you should have the knowledge of the following C++ programming topics:
Example 1: Program to Display Multiplication table up to 10.
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= 10; ++i) {
cout << n << " * " << i << " = " << n * i << endl;
}
return 0;
}
Output
Enter an integer: 5 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50
Working of above Program:
This program above computes the multiplication table up to 10 only.
The program below is the modification of above program in which the user is also asked to entered the range up to which multiplication table should be displayed.
Example 2: Program to Display multiplication table up to a given range
#include <iostream>
using namespace std;
int main()
{
int n, range;
cout << "Enter an integer: ";
cin >> n;
cout << "Enter range: ";
cin >> range;
for (int i = 1; i <= range; ++i) {
cout << n << " * " << i << " = " << n * i << endl;
}
return 0;
}
Output
Enter an integer: 6 Enter range: 12 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
Example 3: Program to Display multiplication table up to 10 using While loop
// C++ Program to Generate Mutiplication Table of a number using While loop.
#include <iostream>
using namespace std;
int main()
{
int num = 1,i = 1;
cout << "Enter the number: ";
cin >> num;
// Loop to Generate muntiplication table of a number
while (i <= 10) {
cout << num <<"x"<< i <<"=" << num * i <<endl;
++i;
}
return 0;
}
Output
Enter the number: 4 4x1=4 4x2=8 4x3=12 4x4=16 4x5=20 4x6=24 4x7=28 4x8=32 4x9=36 4x10=40
Example 4: Program to Display multiplication table up to a given range using while loop
// C++ Program to Generate Multiplecation table of a number using While loop.
#include <iostream>
using namespace std;
int main(){
int n, range, i=1;
cout << "Enter an integer: ";
cin >> n;
cout << "Enter range: ";
cin >> range;
// Loop to Generate muntiplication table of a number using range
while (i <= range) {
cout << n <<"x"<< i <<"=" << n * i <<endl;
++i;
}
return 0;
}
Output
Enter an integer: 3 Enter range: 15 3x1=3 3x2=6 3x3=9 3x4=12 3x5=15 3x6=18 3x7=21 3x8=24 3x9=27 3x10=30 3x11=33 3x12=36 3x13=39 3x14=42 3x15=45
Example 5: Program to Display multiplication table up to 10 using do-While loop
// C++ Program to Generate Mutiplication Table of a number using do-While loop.
#include <iostream>
using namespace std;
int main()
{
int num = 1,i = 1;
cout << "Enter the number: ";
cin >> num;
// do-while Loop to Generate muntiplication table of a number up to 10
do{
cout << num <<"x"<< i <<"=" << num * i <<endl;
++i;
}while (i <= 10);
return 0;
}
Output
Enter the number: 7 7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 7x8=56 7x9=63 7x10=70
Example 6: Program to Display multiplication table up to a given range using do-while loop
// C++ Program to Generate Multiplecation table of a number using While loop.
#include <iostream>
using namespace std;
int main(){
int n, range, i=1;
cout << "Enter an integer: ";
cin >> n;
cout << "Enter range: ";
cin >> range;
// do-while Loop to Generate muntiplication table of a number up to 10
do{
cout << n <<"x"<< i <<"=" << n * i <<endl;
++i;
}while (i <= range);
return 0;
}
Output
Enter an integer: 2 Enter range: 5 2x1=2 2x2=4 2x3=6 2x4=8 2x5=10
Next Example
We hope that this Example helped you develop better understanding of the concept of "Generate Multiplication Table" in C++.
Keep Learning : )
In the next Example, we will learn about C++ Program to Display Fibonacci Sequence
.