Multiplication Table

Program to print multiplication table of a number

BeginnerTopic: Loop Programs
Back

C++ Multiplication Table Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

int main() {
    int num;
    
    cout << "Enter a number: ";
    cin >> num;
    
    cout << "Multiplication table of " << num << ":" << endl;
    for (int i = 1; i <= 10; i++) {
        cout << num << " x " << i << " = " << (num * i) << endl;
    }
    
    return 0;
}
Output
Enter a number: 5
Multiplication table of 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

Understanding Multiplication Table

This program generates a multiplication table for any given number. It demonstrates the for loop, which is one of the most fundamental concepts in programming. Multiplication tables are essential for learning arithmetic and understanding repetitive operations in programming.

---

1. What This Program Does

The program:

Takes a number as input from the user
Generates and displays its multiplication table from 1 to 10
Shows how the input number multiplies with numbers 1 through 10

This is a classic beginner program that teaches loops, which are used in almost every real-world application.

---

2. Header File: #include <iostream>

#include <iostream>

This header provides:

cout → for displaying output
cin → for reading input from the user

Essential for any program that needs user interaction.

---

3. Declaring Variables

int num;

int is the data type for whole numbers (integers)
num stores the number entered by the user
This is the number for which we'll generate the multiplication table

Example:

If user enters

5

, then num = 5

The table will show: 5 × 1, 5 × 2, 5 × 3, ..., 5 × 10

---

4. Taking Input From User

`cout << "Enter a number: ";`

cin >> num;

First line displays a prompt message
Second line reads the number and stores it in num

Example:

User sees: "Enter a number: "
User types:

5

num now contains

5

---

5. Displaying Table Header

`cout << "Multiplication table of " << num << ": " << endl;`

This line:

Prints "Multiplication table of "
Prints the value of num (e.g., 5)
Prints ":"
endl moves to the next line

Output:

Multiplication table of 5:

---

6. Understanding the For Loop

for (int i = 1; i <= 10; i++)

The for loop is a control structure that repeats a block of code multiple times.

Syntax breakdown:

for (initialization; condition; increment) {

}

    // code to repeat

Parts of this for loop:

1.

Initialization: `int i = 1`

Creates a variable i and sets it to 1
This is the starting point of our loop
i is called the "loop counter" or "iterator"

2.

Condition: `i <= 10`

Checks if i is less than or equal to 10
If

true

, the loop continues

If

false

, the loop stops

3.

Increment: `i++`

Increases i by 1 after each iteration
i++ is shorthand for i = i + 1

How the loop works:

-

Iteration 1:

i = 1, check 1 <= 10

true

→ execute code

-

Iteration 2:

i = 2, check 2 <= 10

true

→ execute code

-

Iteration 3:

i = 3, check 3 <= 10

true

→ execute code

...

-

Iteration 10:

i = 10, check 10 <= 10

true

→ execute code

-

Iteration 11:

i = 11, check 11 <= 10

false

loop stops

---

7. Loop Body - Calculating and Displaying

{

}

This code runs 10 times (once for each value of i from 1 to 10).

    cout << num << " x " << i << " = " << (num * i) << endl;

What happens in each iteration:

When i = 1:

num = 5, i = 1
Calculates: num * i = 5 * 1 = 5
Prints:

5 x 1 = 5

When i = 2:

num = 5, i = 2
Calculates: num * i = 5 * 2 = 10
Prints:

5 x 2 = 10

When i = 3:

Calculates: 5 * 3 = 15
Prints:

5 x 3 = 15

And so on...

Breaking down the output statement:

cout << num → prints the number (5)
<< " x " → prints the text " x "
<< i → prints the loop counter (1, 2, 3, ...)
<< " = " → prints the text " = "
<< (num * i) → prints the result of multiplication
<< endl → moves to next line

---

8. Complete Execution Flow

Input:

User enters

5

Step 1:

num = 5

Step 2:

Print header: "Multiplication table of 5:"

Step 3:

Start loop

-

i = 1:

Print "5 x 1 = 5"

-

i = 2:

Print "5 x 2 = 10"

-

i = 3:

Print "5 x 3 = 15"

-

i = 4:

Print "5 x 4 = 20"

-

i = 5:

Print "5 x 5 = 25"

-

i = 6:

Print "5 x 6 = 30"

-

i = 7:

Print "5 x 7 = 35"

-

i = 8:

Print "5 x 8 = 40"

-

i = 9:

Print "5 x 9 = 45"

-

i = 10:

Print "5 x 10 = 50"

-

i = 11:

Condition false → loop ends

Step 4:

return 0; → program ends

---

9. Why Use a Loop?

Without loop (inefficient way):

This requires writing 10 similar lines of code.

cout << num << " x 1 = " << (num * 1) << endl;
cout << num << " x 2 = " << (num * 2) << endl;
cout << num << " x 3 = " << (num * 3) << endl;
// ... repeat 10 times

With loop (efficient way):

Write the code once
Loop repeats it 10 times automatically
Easy to change range (e.g., 1 to 20 instead of 1 to 10)

---

10. Modifying the Range

To generate table up to 20 instead of 10:

Change: i <= 10 to i <= 20

To generate table from 5 to 15:

Change: int i = 1 to int i = 5
Change: i <= 10 to i <= 15

---

Summary

For loops repeat code multiple times efficiently
Loop counter (i) changes automatically in each iteration
Multiplication is performed inside the loop using num * i
This pattern is used in many programs: printing patterns, processing arrays, calculations
Understanding loops is essential for solving complex programming problems

This program is fundamental because loops are one of the most important concepts in programming. Once you master loops, you can solve problems involving repetition, iteration, and processing multiple items.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Multiplication Table

This C++ program is part of the "Loop Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents