C++ Program to Display Multiplication Table of a Given Number

Multiplication table

C++ programs to display the multiplication table of a number have been shown here. Both the iterative and recursive approaches have been covered below.






1. C++ Program & output to display multiplication table of a number using iteration

Code has been copied
/**************************************
	  alphabetacoder.com
C++ Program to display multiplication
table of a given number using iteration
***************************************/

#include<iostream>

using namespace std;

int main() {
    // delclare variables
    int i, n, r;

    // take input of a number
    cout << "Enter a number: ";
    cin >> n;
    cout << "Enter the range: ";
    cin >> r;

    cout << endl << "Multiplication table of " << n << ": " << endl;
    // display table
    for (i = 1; i <= r; i++) {
        cout << n << " x " << i << " = " << n * i << endl;
    }

    return 0;
}

Output


Enter a number: 7

Enter the range: 10

Multiplication table of 7:

7 x 1 = 7

7 x 2 = 14

7 x 3 = 21

7 x 4 = 28

7 x 5 = 35

7 x 6 = 42

7 x 7 = 49

7 x 8 = 56

7 x 9 = 63

7 x 10 = 70





2. C++ Program & output to display multiplication table of a number using recursion

Code has been copied
/***************************************
	  alphabetacoder.com
C++ Program to display multiplication
table of a given number using recursion
****************************************/

#include<iostream>

using namespace std;

// recursive function to display 
// multiplication table
void multiplication_table(int n, int r) {
    // exit condition of recursive call
    if (r == 0)
        return;
    // call function
    multiplication_table(n, r - 1);
    // display the line
    cout << n << " x " << r << " = " << n * r << endl;
}

int main() {
    // delclare variables
    int n, r;

    // take input of a number
    cout << "Enter a number: ";
    cin >> n;
    cout << "Enter the range: ";
    cin >> r;

    cout << endl << "Multiplication table of " << n << ": " << endl;
    // call function to display multiplication table
    multiplication_table(n, r);

    return 0;
}

Output


Enter a number: 9

Enter the range: 12

Multiplication table of 9:

9 x 1 = 9

9 x 2 = 18

9 x 3 = 27

9 x 4 = 36

9 x 5 = 45

9 x 6 = 54

9 x 7 = 63

9 x 8 = 72

9 x 9 = 81

9 x 10 = 90

9 x 11 = 99

9 x 12 = 108