Sample Example
Say the number given to us is 5.
So the compiler should print output as:
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
Now let us see how we did it, its approach, algorithm implementation, and complexities.
Approach
Our strategy will be simple and straightforward. First, we'll just declare a for loop so that we can multiply the given number 10 times to print the multiplication table. Then we need the multiplication operator(asterisk) to actually multiply the given number.
Algorithm
Step 1: Declare a number whose multiplication table has to be printed.
Step 2: Declare a for loop for repetitive multiplication.
Step 3: Display the multiplication table.
Implementation in C++
C++
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter the given number :" << endl;
cin >> number;
//declaring for loop for repetitive multiplication
for (int i = 1; i <= 10; i++)
{
cout << number << " * " << i << " = " << number * i << endl;
}
}
You can also try this code with Online C++ Compiler
Run Code
Output
Enter the given number :
5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Let us analyze the time and space complexity of this approach.
Complexity analysis
Time complexity: O(N)
This approach will take O(N), where N is the total number of times the loop has to be executed. Here it is 10.
Space complexity: O(1)
In this approach, no extra space is required.
Also Read - C++ Interview Questions
Different Ways to Generate the Table Program
- Using for loop.
- Using while loop.
- Using do-while loop.
- Using a for loop with a user-defined function.
- Using recursion.
- Using goto statement.
- Using pointers.
- Generating tables for multiple numbers (e.g., 2 to 10) using nested loops.
Program to Generate the Table of a Given Number Using For Loop
C++
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number to generate its table: ";
cin >> num;
for (int i = 1; i <= 10; ++i) {
cout << num << " x " << i << " = " << num * i << endl;
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
For input 5:
Enter a number to generate its table: 5
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
Program to Generate the Table of a Number Using While Loop
C++
#include <iostream>
using namespace std;
int main() {
int num, i = 1;
cout << "Enter a number to generate its table: ";
cin >> num;
while (i <= 10) {
cout << num << " x " << i << " = " << num * i << endl;
++i;
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
For input 4:
Enter a number to generate its table: 4
4 x 1 = 4
4 x 2 = 8
...
4 x 10 = 40
Program to Generate the Table Using For Loop and User-Defined Function
C++
#include <iostream>
using namespace std;
void generateTable(int num) {
for (int i = 1; i <= 10; ++i) {
cout << num << " x " << i << " = " << num * i << endl;
}
}
int main() {
int num;
cout << "Enter a number to generate its table: ";
cin >> num;
generateTable(num);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
For input 3:
Enter a number to generate its table: 3
3 x 1 = 3
3 x 2 = 6
...
3 x 10 = 30
Program to Generate the Table of a Number Using Do-While Loop
C++
#include <iostream>
using namespace std;
int main() {
int num, i = 1;
cout << "Enter a number to generate its table: ";
cin >> num;
do {
cout << num << " x " << i << " = " << num * i << endl;
++i;
} while (i <= 10);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
For input 7:
Enter a number to generate its table: 7
7 x 1 = 7
7 x 2 = 14
...
7 x 10 = 70
Program to Generate the Table of a Number Using Recursion
C++
#include <iostream>
using namespace std;
void generateTable(int num, int i) {
if (i > 10) return;
cout << num << " x " << i << " = " << num * i << endl;
generateTable(num, i + 1);
}
int main() {
int num;
cout << "Enter a number to generate its table: ";
cin >> num;
generateTable(num, 1);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
For input 6:
Enter a number to generate its table: 6
6 x 1 = 6
6 x 2 = 12
...
6 x 10 = 60
Program to Generate the Table of a Number Using Goto Statement
C++
#include <iostream>
using namespace std;
int main() {
int num, i = 1;
cout << "Enter a number to generate its table: ";
cin >> num;
table:
if (i > 10) return 0;
cout << num << " x " << i << " = " << num * i << endl;
++i;
goto table;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
For input 2:
Enter a number to generate its table: 2
2 x 1 = 2
2 x 2 = 4
...
2 x 10 = 20
Program to Generate the Table of a Number Using Pointer
C++
#include <iostream>
using namespace std;
void generateTable(int *num) {
for (int i = 1; i <= 10; ++i) {
cout << *num << " x " << i << " = " << (*num) * i << endl;
}
}
int main() {
int num;
cout << "Enter a number to generate its table: ";
cin >> num;
generateTable(&num);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
For input 8:
Enter a number to generate its table: 8
8 x 1 = 8
8 x 2 = 16
...
8 x 10 = 80
Program to Generate the Table from 2 to 10 Using Nested For Loop
C++
#include <iostream>
using namespace std;
int main() {
for (int num = 2; num <= 10; ++num) {
cout << "Table of " << num << ":\n";
for (int i = 1; i <= 10; ++i) {
cout << num << " x " << i << " = " << num * i << endl;
}
cout << endl;
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Table of 2:
2 x 1 = 2
...
2 x 10 = 20
Table of 3:
3 x 1 = 3
...
3 x 10 = 30
...
Table of 10:
10 x 1 = 10
...
10 x 10 = 100
Frequently Asked Questions
Mention different types of loops present in the programming language.
There are 3 different types of loops present in any programming language:
- For loop
- While loop
- Do-while loop
What is the limitation of using loops?
The limitation that exists with loops is that sometimes there exist infinite loops. These loops give the same answer over and again. Then, we need to use control statements to get out of such a loop. Control statements are continue, break, etc.
What are the essential parts of a loop?
Loop consists of three essential parts. They are:
- Beginning of the loop. The initialization part.
- The condition part decides where the loop should run.
- The termination part generally includes incrementation and decrementation statements.
Conclusion
To conclude this blog, firstly we discussed the problem statement and ways to solve the problems. For the approach, we discussed its algorithm, pseudo-code implementation, and complexities.