Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Problem Statement
1.2.
Sample Example
2.
Approach
2.1.
Algorithm
2.2.
Implementation in C++
2.2.1.
Complexity analysis
3.
Frequently Asked Questions
3.1.
Mention different types of loops present in the programming language.
3.2.
What is the limitation of using loops?
3.3.
What are the essential parts of a loop?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

C++ program to Print Table of any Number

Author Urwashi Priya
0 upvote

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

#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;
    }
}

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

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. 

Also read - Decimal to Binary c++

Refer to our guided paths on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must have a look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass