#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
Multiplication Table in C++
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.
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.
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:
cppfor (initialization; condition; increment) { // code to repeat }
Parts of this for loop:
-
Initialization:
int i = 1- Creates a variable
iand sets it to 1 - This is the starting point of our loop
iis called the "loop counter" or "iterator"
- Creates a variable
-
Condition:
i <= 10- Checks if
iis less than or equal to 10 - If ## true, the loop continues
- If ## false, the loop stops
- Checks if
-
Increment:
i++- Increases
iby 1 after each iteration i++is shorthand fori = i + 1
- Increases
How the loop works:
-
Iteration 1:
i = 1, check1 <= 10→ ## true → execute code -
Iteration 2:
i = 2, check2 <= 10→ ## true → execute code -
Iteration 3:
i = 3, check3 <= 10→ ## true → execute code - ...
-
Iteration 10:
i = 10, check10 <= 10→ ## true → execute code -
Iteration 11:
i = 11, check11 <= 10→ ## false → ## loop stops
Loop Body - Calculating and Displaying
cpp{ cout << num << " x " << i << " = " << (num * i) << endl; }
This code runs 10 times (once for each value of i from 1 to 10).
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
And so on...
Why Use a Loop?
Without loop (inefficient way):
cppcout << num << " x 1 = " << (num * 1) << endl; cout << num << " x 2 = " << (num * 2) << endl; cout << num << " x 3 = " << (num * 3) << endl; // ... repeat 10 times
This requires writing 10 similar lines of code.
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)
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.