Write a C++ Program to Generate Multiplication Table
Generating a multiplication table is a basic and important program for beginners in C++. It helps students understand loops, user input, and arithmetic operations. In this tutorial by cslearning.org, we will learn how to write a simple and student-friendly C++ program to generate the multiplication table of a given number.
This guide includes introduction, logic, simple C++ source code, sample output, and viva questions with answers.
1. Introduction of Multiplication Table
A multiplication table shows the result of multiplying a number with a series of integers. It is commonly used in mathematics to learn multiplication.
For example, the multiplication table of 5 is:
5 × 1 = 5
5 × 2 = 10
5 × 3 = 15
…
5 × 10 = 50
In programming, we can generate this table using loops.
2. Logic of the Program
To generate a multiplication table in C++, follow these steps:
- Start the program.
- Declare an integer variable.
- Take input from the user.
- Use a loop from 1 to 10.
- Multiply the given number with the loop variable.
- Display the result in table format.
Example:
If the user enters 4
4 × 1 = 4
4 × 2 = 8
4 × 3 = 12
…
4 × 10 = 40
The loop repeats 10 times to print the full table.
3. C++ Source Code (Easy Student Friendly Code)
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
for(int i = 1; i <= 10; i++)
{
cout << num << " x " << i << " = " << num * i << endl;
}
return 0;
}
Explanation:#include <iostream> is used for input and output.
The for loop runs from 1 to 10.
Inside the loop, the number is multiplied by the loop variable i.
Each result is printed in multiplication table format.
4. Sample Output
Example:
Enter a number: 6
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
5. Viva Questions with Answers
Q1: What is a multiplication table?
Answer: A multiplication table shows the results of multiplying a number by a sequence of integers.
Q2: Which loop is used in this program?
Answer: A for loop is used to generate the table.
Q3: How many times does the loop run in this program?
Answer: The loop runs 10 times.
Q4: Can we change the range of the table?
Answer: Yes, we can change the loop condition to print more or fewer values.
Q5: Which operator is used for multiplication in C++?
Answer: The * operator is used for multiplication.
This program helps beginners understand loops and arithmetic operations in C++. Practice more C++ programs on cslearning.org to improve your programming skills.
