In this example, you will learn to write a C++ program for generating a multiplication table of numbers entered by a user.
Example:
input: 5 output: 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
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter the number: ";
cin >> num;
for (int i = 1; i <= 10; i++) {
cout << num << " * " << i << " = " << num * i << endl;
}
return 0;
}
Enter the number: 12 12 * 1 = 12 12 * 2 = 24 12 * 3 = 36 12 * 4 = 48 12 * 5 = 60 12 * 6 = 72 12 * 7 = 84 12 * 8 = 96 12 * 9 = 108 12 * 10 = 120
Explanation:
In this multiplication table program, you will take a number from the user and store it in a variable num
.
Now iterate the loop from 1 to 10, inside the loop multiply num with the current iteration i.e (num * i
).
In this program, we will generate a multiplication of table of multiple numbers by taking lower range and higher range from the user.
#include <iostream>
using namespace std;
int main()
{
int number, enumber;
cout << "enter the starting range number: ";
cin >> snumber;
cout << "enter the ending range number: ";
cin >> enumber;
for(int i = snumber; i<=enumber; i++){
for (int j = 1; j <= 10; j++) {
cout<< i * j<< "\t ";
}
cout<<endl;
}
return 0;
}
enter the starting range number: 1 enter the ending range number: 10 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100