# C++ Program to Print Multiplication Table of Number

« Previous Program Next Program »

In this article, you will learn and get code to print multiplication table of number using C++ program. Here are the list of programs on printing of multiplication table:

• Print Multiplication Table of 2
• Print Multiplication Table of any Given Number
• Print Multiplication Table from 1 to 10
• Print Multiplication Table of all Numbers in a Given Range

### What is a Multiplication Table ?

A multiplication table shows a list of multiples of a particular number, from 1 to 10. For example, the multiplication table of 3 will be:

```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```

Note - Typically list of multiples are from 1 to 12. But in this article, we've created all the programs on printing of multiplication table based on multiples from 1 to 10 only.

## Print Multiplication Table of 2

```#include<iostream>
using namespace std;
int main()
{
int num=2, i, res;
for(i=1; i<=10; i++)
{
res = num*i;
cout<<num<<" * "<<i<<" = "<<res;
cout<<endl;
}
cout<<endl;
return 0;
}```

This program was build and run under Code::Blocks IDE. Here is its sample output:

The dry run of this program goes like:

• Initial value, num=2
• Now the execution of for loop begins. Its initialization (first statement) gets executed at first but only at once
• So 1 gets initialized to i and the condition i<=10 or 1<=10 evaluates to be true, therefore program flow goes inside the loop
• And num*i or 2*1 or 2 gets initialized to res
• Now using the following statement:
`cout<<num<<" * "<<i<<" = "<<res;`
2 * 1 = 2, gets printed on output
• The third statement, that is,
`cout<<endl;`
gets executed, that prints a newline, that is next output thing starts from new line
• Now the program flow goes to the update (third statement) of for loop and increments the value of i. So i=2
• The condition i<=10 or 2<=10 again evaluates to be true, therefore program flow again goes inside the loop
• This process continues, until its condition evaluates to be false
• In this way, the multiplication table of 2 gets printed on output screen.

## Print Multiplication Table of any Given Number

To print table of any given number in C++ programming, you have to ask from user to enter the number. Then start multiplying that number from 1 to 10, one by one, and display the multiplication result at the time of multiplying, as shown here in the following program.

```#include<iostream>
using namespace std;
int main()
{
int num, i, res;
cout<<"Enter the Number: ";
cin>>num;
for(i=1; i<=10; i++)
{
res = num*i;
cout<<num<<" * "<<i<<" = "<<res;
cout<<endl;
}
cout<<endl;
return 0;
}```

Here is the initial output produced by this C++ program:

Now supply the input say 5 and press `ENTER` key to print its table as shown in the snapshot given below:

## Print Multiplication Table from 1 to 10

This program prints table from 1 to 10. That is, first table of 1 gets printed, then table of 2 gets printed, and so on upto table of 10:

```#include<iostream>
using namespace std;
int main()
{
int num, i, j, res;
cout<<"-----------Table from 1 to 10--------\n\n";
for(i=1; i<=10; i++)
{
num = i;
cout<<"Table of "<<num<<": ";
for(j=1; j<=10; j++)
{
res = num*j;
cout<<res<<"  ";
}
cout<<endl;
}
cout<<endl;
return 0;
}```

Here is the sample output produced by this program:

## Print Multiplication Table in a Given Range

This program allows user to enter the range to print table of all numbers in that given range. For example, if user enters range as 3 and 15, then table of all numbers from 3 to 15 gets printed.

```#include<iostream>
using namespace std;
int main()
{
int numOne, numTwo, num, i, j, res;
cout<<"Enter the Range: ";
cin>>numOne>>numTwo;
cout<<"--------Table from "<<numOne<<" to "<<numTwo<<"--------\n\n";
for(i=numOne; i<=numTwo; i++)
{
num = i;
cout<<"Table of "<<num<<": ";
for(j=1; j<=10; j++)
{
res = num*j;
cout<<res<<"  ";
}
cout<<endl;
}
cout<<endl;
return 0;
}```

Here is its sample run with user input, 3 as first and 15 as second number:

Note - What if user enters two numbers in a way that first number is greater than the second ?

To solve this problem, use this program, instead of previous one:

```#include<iostream>
using namespace std;
int main()
{
int numOne, numTwo, num, i, j, res;
cout<<"Enter the Range: ";
cin>>numOne>>numTwo;
if(numTwo>numOne)
{
cout<<"--------Table from "<<numOne<<" to "<<numTwo<<"--------\n\n";
for(i=numOne; i<=numTwo; i++)
{
num = i;
cout<<"Table of "<<num<<": ";
for(j=1; j<=10; j++)
{
res = num*j;
cout<<res<<"  ";
}
cout<<endl;
}
}
else
{
cout<<"--------Table from "<<numTwo<<" to "<<numOne<<"--------\n\n";
for(i=numTwo; i<=numOne; i++)
{
num = i;
cout<<"Table of "<<num<<": ";
for(j=1; j<=10; j++)
{
res = num*j;
cout<<res<<"  ";
}
cout<<endl;
}
}
cout<<endl;
return 0;
}```

Here is its sample run with user input, 20 as first and 6 as second number:

#### Same Program in Other Languages

C++ Online Test

« Previous Program Next Program »