codescracker
c++

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:

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

Let's start with very first program of this article, that prints 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:

C++ program print table of number

The dry run of this program goes like:

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:

print table of any number c++

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

print table of given number c++

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 table from 1 to 10 c++

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:

print table in given range c++

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:

c++ print table

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker