0
0
CppProgramBeginner · 2 min read

C++ Program to Print Multiplication Table

Use a for loop in C++ to print the multiplication table by multiplying the given number with numbers from 1 to 10, like for(int i = 1; i <= 10; i++) std::cout << n << " x " << i << " = " << n*i << std::endl;.
📋

Examples

Input5
Output5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
Input1
Output1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8 1 x 9 = 9 1 x 10 = 10
Input0
Output0 x 1 = 0 0 x 2 = 0 0 x 3 = 0 0 x 4 = 0 0 x 5 = 0 0 x 6 = 0 0 x 7 = 0 0 x 8 = 0 0 x 9 = 0 0 x 10 = 0
🧠

How to Think About It

To print a multiplication table, think of multiplying the given number by each number from 1 to 10. Use a loop that counts from 1 to 10, and in each step, multiply the input number by the loop counter and print the result in a clear format.
📐

Algorithm

1
Get the number for which the multiplication table is needed
2
Start a loop from 1 to 10
3
In each loop iteration, multiply the number by the loop counter
4
Print the multiplication expression and result
5
Repeat until the loop ends
💻

Code

cpp
#include <iostream>

int main() {
    int n;
    std::cout << "Enter a number: ";
    std::cin >> n;
    for (int i = 1; i <= 10; i++) {
        std::cout << n << " x " << i << " = " << n * i << std::endl;
    }
    return 0;
}
Output
Enter a number: 5 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
🔍

Dry Run

Let's trace the program with input 5 through the code

1

Input number

User enters 5, so n = 5

2

Start loop

Loop counter i starts at 1

3

Calculate and print

Print '5 x 1 = 5' because 5 * 1 = 5

4

Increment loop

i becomes 2

5

Repeat calculation

Print '5 x 2 = 10' because 5 * 2 = 10

6

Continue loop

Loop continues until i = 10

7

End loop

After printing '5 x 10 = 50', loop ends

iExpressionResult
15 x 15
25 x 210
35 x 315
45 x 420
55 x 525
65 x 630
75 x 735
85 x 840
95 x 945
105 x 1050
💡

Why This Works

Step 1: Input the number

The program asks the user to enter a number and stores it in variable n.

Step 2: Loop from 1 to 10

A for loop runs from 1 to 10 to cover all multipliers for the table.

Step 3: Print multiplication

Inside the loop, the program multiplies n by the loop counter i and prints the result in a readable format.

🔄

Alternative Approaches

Using while loop
cpp
#include <iostream>

int main() {
    int n, i = 1;
    std::cout << "Enter a number: ";
    std::cin >> n;
    while (i <= 10) {
        std::cout << n << " x " << i << " = " << n * i << std::endl;
        i++;
    }
    return 0;
}
This uses a <code>while</code> loop instead of <code>for</code>, which is equally clear but slightly longer.
Printing table for multiple numbers
cpp
#include <iostream>

int main() {
    for (int num = 1; num <= 3; num++) {
        std::cout << "Table for " << num << ":\n";
        for (int i = 1; i <= 10; i++) {
            std::cout << num << " x " << i << " = " << num * i << std::endl;
        }
        std::cout << std::endl;
    }
    return 0;
}
This prints multiplication tables for numbers 1 to 3, showing nested loops usage.

Complexity: O(1) time, O(1) space

Time Complexity

The loop runs exactly 10 times, so the time is constant, O(1), regardless of input size.

Space Complexity

Only a few variables are used, so space is constant, O(1).

Which Approach is Fastest?

All approaches use simple loops with fixed iterations, so they have similar performance.

ApproachTimeSpaceBest For
For loopO(1)O(1)Simple and clear multiplication tables
While loopO(1)O(1)Alternative looping style
Nested loops for multiple tablesO(1)O(1)Printing tables for multiple numbers
💡
Use a loop from 1 to 10 and multiply the input number by the loop counter to print the table.
⚠️
Beginners often forget to increment the loop counter, causing an infinite loop.