In this example, I’ll show you How to Generate Multiplication Table.
C++ program to print multiplication table of a number entered by a user using a for
loop. You can modify it for while
or do while
loop for practice. Using nested loops (a loop inside another loop), we can print tables of numbers between a given range say a to b, for example, if the input numbers are ‘3’ and ‘6’ then tables of ‘3’, ‘4’, ‘5’ and ‘6’ will be printed.
To print table of a number in C++ programming, you have to ask to the user to enter a number and start multiplying that number from 1 to 10 and display the multiplication result at the time of multiplying on the output screen which is the table of the number as shown here in the following program.
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, i, tab; cout<<"Enter a number : "; cin>>num; cout<<"Table of "<<num<<" is \n\n"; for(i=1; i<=10; i++) { tab=num*i; cout<<num<<" * "<<i<<" = "<<tab<<"\n"; } } |
Example 2: Display multiplication table up to a given range
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #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: