Multiplication Table
Program to print multiplication table of a number
C++ Multiplication Table Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}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:
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 outputcin → for reading input from the userEssential 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 userExample:
5
, then num = 5
---
4. Taking Input From User
cin >> num;
numExample:
5
num now contains5
---
5. Displaying Table Header
This line:
num (e.g., 5)endl moves to the next lineOutput:
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 repeatParts of this for loop:
1.
Initialization: `int i = 1`
i and sets it to 1i is called the "loop counter" or "iterator"2.
Condition: `i <= 10`
i is less than or equal to 10true
, the loop continues
false
, the loop stops
3.
Increment: `i++`
i by 1 after each iterationi++ is shorthand for i = i + 1How 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 = 1num * i = 5 * 1 = 55 x 1 = 5
When i = 2:
num = 5, i = 2num * i = 5 * 2 = 105 x 2 = 10
When i = 3:
5 * 3 = 155 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:
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 timesWith loop (efficient way):
---
10. Modifying the Range
To generate table up to 20 instead of 10:
i <= 10 to i <= 20To generate table from 5 to 15:
int i = 1 to int i = 5i <= 10 to i <= 15---
Summary
i) changes automatically in each iterationnum * iThis 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.