Table of contents
1.
Introduction
2.
Problem Statement
3.
Sample Example
4.
Approach
4.1.
Algorithm
4.2.
Implementation in C++
4.3.
C++
4.3.1.
Complexity analysis
5.
Different Ways to Generate the Table Program
5.1.
Program to Generate the Table of a Given Number Using For Loop
5.2.
C++
5.3.
Program to Generate the Table of a Number Using While Loop
5.4.
C++
5.5.
Program to Generate the Table Using For Loop and User-Defined Function
5.6.
C++
5.7.
Program to Generate the Table of a Number Using Do-While Loop
5.8.
C++
5.9.
Program to Generate the Table of a Number Using Recursion
5.10.
C++
5.11.
Program to Generate the Table of a Number Using Goto Statement
5.12.
C++
5.13.
Program to Generate the Table of a Number Using Pointer
5.14.
C++
5.15.
Program to Generate the Table from 2 to 10 Using Nested For Loop
5.16.
C++
6.
Frequently Asked Questions
6.1.
Mention different types of loops present in the programming language.
6.2.
What is the limitation of using loops?
6.3.
What are the essential parts of a loop?
7.
Conclusion
Last Updated: Jan 14, 2025
Easy

C++ program to Print Table of any Number

Author Urwashi Priya
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will be discussing a simple loop problem. Loops are just used to iterate a part of a statement for a given specific time. Instead of writing the same thing repetitively, we bring the loop into the picture. 

Program to Print Table of Any Number

Problem Statement

In this problem, we are given a number and we just need to print the multiplication table of the given number.

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++

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

  1. Using for loop.
  2. Using while loop.
  3. Using do-while loop.
  4. Using a for loop with a user-defined function.
  5. Using recursion.
  6. Using goto statement.
  7. Using pointers.
  8. 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++

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++

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++

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++

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++

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++

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++

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++

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:

  1. For loop
  2. While loop
  3. 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:

  1. Beginning of the loop. The initialization part.
  2. The condition part decides where the loop should run.
  3. 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. 

Live masterclass