×

Print Table Using Do while Loop in C++

Multiplication Table

In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is also referred to as a multiplication table. Suppose, we need to create a table of the number 7, then we have to multiply 7 by each natural number as follows:

7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35

What is Do-While Loop?

An iterative loop that checks the condition at the end.
The Do-While loop can be used whenever a test condition is specific, as the control enters the loop at a minimum once before the condition is evaluated. This loop checks the given condition after the execution of looping statements.

The loop actions or statements will be repeated infinitely as long as the test requirements are met.

Note: DO-WHILE LOOP means “first do it, then check”.

Syntax:

The Syntax of the DO-WHILE loop is:

Do
           { 
            statement(s);
           } 
         while(expression);

Program to print multiplication of 2 tables from (1 to 12)

Algorithm

Step 1: Start

Step 2: Read n, res, I;

Step 3: Take a number “n” from the user to print the table

Step 4: Use Do While loop to print table. Initialize (i=1) and check condition until i becomes 12. when, the condition becomes wrong, then control moves to step 6.

Step 5: res=num*i;

Step 6: Print “res”.

Step 7: End.

Program

#include<iostream>
using namespace std;
int main()
{
    int num=2, i, res;
   i=1; 
do{
        res = num*i;
        cout<<num<<" * "<<i<<" = "<<res;
        cout<<endl;
i++;
    }while(i<12);
    cout<<endl;
    return 0;
}

Output

2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14 
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
2 * 11 = 22
2 * 12 = 24

Explanation

The above code prints the table of 2.
The do while loop is used in this program. And, the control enter in the loop and execute the operation and stores the value of num*i, i.e., 2*1, i.e., 2.

Then, the following statement will exexute:
cout<<num<<" * "<<i<<" = "<<res;

The above statement prints the first line like this 2 *1 = 2 in the output. In the loop, (cout<<endl) is next statement that indicates the program will write the output from new line.

Initially, value of i is initialized to 1, and the update statement which increases the value of i. After that, the condition checks and evaluates to true because (2<12), which allows the program to enter in loop again.
This process will continue until the condition is determined to be untrue.
The multiplication table of 2 is printed on the output screen.

Program to print a table of any user-defined number

#include<iostream>
using namespace std;
int main()
{
    int num, i, res;
    cout<<"Enter the Number: ";
    cin>>num;
    i=1;
    do{
        res = num*i;
        cout<<num<<" * "<<i<<" = "<<res;
        cout<<endl;
    i++;
} while(i<=10);
    cout<<endl;
    return 0;
}

Output

Enter a number: 10
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60 
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100

Program to print table from 1 to 10

Algorithm

Step 1: Start the program.

Step 2: Read n, i=1, j=1.

Step 3: Use two do-while loops to print the tables

               Iterate the value of i until i=10

                  à in the inner loop itearate j until (j<=n) else go to step4

 Step 4: If (j<n-1)

                 Cout<<j<<”x”<<i<<i*j

               Else

                 Cout<<j<<”x”<<i<<i*j

Step 5: End the program.

Flowchart:

Print Table Using Do-While loop

Program

#include <iostream>
using namespace std;
int main()
{
    int j, i, n;
   cout << "\n\n the multipliaction table from 1 to n:\n";
    cout << "-------------------------------------------------------------\n";
    cout << "Input the number upto 5: ";
    cin >> n;
    cout << "Multiplication table from 1 to " << n << endl;
    i=1;
   do {
        j=1;
        do{
            if (j<=n-1)
                cout<<j<<"x"<<i<<"="<<i*j<<"\t";
            else
                cout<<j<<"x"<<i<< "=" <<i*j<<endl;
        j++;
    }while(j<=n);
i++;
}while(i<=10);
        cout << endl;
     }

Output

Enter any number: 
The multiplication table from 1 to 4
1x1=1    2x1=2      3x1=3         4x1=4  
1x2=2    2x2=4      3x2=6         4x2=8  
1x3=3    2x3=6      3x3=9         4x3=12  
1x4=4    2x4=8      3x4=12        4x4=16                                  
1x5=5    2x5=10    3x5=15         4x5=20                                
1x6=6    2x6=12   3x6=18          4x6=24                                
1x7=7    2x7=14   3x7=21          4x7=28                                  
1x8=8    2x8=16   3x8=24          4x8=32                                
1x9=9    2x9=18   3x9=27          4x9=36                              
1x10=10  2x10=20  3x10=30         4x10=40  

Program to print table using recursive:

#include <iostream>
using namespace std;
 void mul_table(int N, int i)
{
    // Base Case
    if (i > 10)
        return;
       cout << N << " * " << i << " = " << N * i << endl;
      return mul_table(N, i + 1);
}
 
// Driver Code
int main()
{
    int N = 7;
    mul_table(N, 1);
    return 0;
}

Output:

7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 48
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70

Related Topics

Add two numbers represented by two arrays in C++

The array stores a number in such a way that each digit of the number is represented by an array element. As an example, The array number 147 is 1,4,7. To add...

3 minutes read.

OOPs Concepts in C++

C++ Object-Oriented Programming Concepts C++ uses the concept of object-oriented programming. Object Oriented Programming has some prominent features: Object Class Data abstraction Encapsulation Polymorphism Inheritance Message passing Object An object is the basic unit...

2 minutes read.

How to concatenate two strings in C++

In the C++ programming language, the concatenation of two or even more strings is covered in this section. The term "string concatenation" refers to a collection of characters that join two...

4 minutes read.

Difference between Exit and Return

Define Exit() At the point when a client needs to leave a program from this capability is utilized. A void return type capability calls all capabilities enrolled at the exit and ends...

3 minutes read.

Type conversion in C++

Type conversion is the process of changing the type of variables in a program. The ultimate goal of type conversions is to allow variables of a single data type operate with...

7 minutes read.

Exception Handling in C++ vs Java

Nowadays, exception handling is a feature found in virtually all object-oriented languages. We can also find this type of feature in Java and C++. The try-catch and block is required...

3 minutes read.

How to Use Getline in C++

What is getline in C++? Similar to the cin function, which allows the programmer to take input from the user getline() function also takes input from the user. But in this...

4 minutes read.

C++ Encapsulation

The following two essential components are present in all C++ programmes: Functions are the parts of a programme that perform actions, and they are termed programme statements (code).Program data is the...

4 minutes read.

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 minutes read.

Features and Use of Pointers in C/C++

What is a pointer? A pointer is mainly used to store the address of another variable. The * operator creates a pointer variable, which points to a data type (like an...

7 minutes read.

Approach in C++

Object oriented programming languages like Java or C++ use a bottom-up approach that identifies each object first.  In the bottom-up approach, we create a small problem first, and try to...

6 minutes read.

How to find the length of the vector in C++

Like dynamic arrays, vectors can automatically adjust their size when an element is added or removed, and the container manages its storage. Because vector items are stored in contiguous storage, iterators...

3 minutes read.

Single dimension array

C++ Array An array is a collection of data (elements) of the same data types. The elements of an array are allocated in contiguous memory allocation. Elements of the array are accessed through...

1 minute read.

This keyword in C++

This keyword in C++ is a pointer which points to the object. It is passed as an argument to functions which helps in accessing the object. It is used for a...

3 minutes read.

C++ add two numbers using the function

Here we will learn how to add two numbers by creating function in C++. Let’s learn this with help of example. Code: - #include <iostream> using namespace std; int add_two_no(int a, int b); int main(){   int...

1 minute read.

Program that produces different results in C and C++

Introduction: There are many such programs that compile run both in C and C++ but give different outcomes when compiled by the C and C++ compilers. There are a variety of such...

6 minutes read.

C++ Interfaces

The C++ programming language provides programmers with a variety of capabilities and functions. It also enables object-oriented programming, which is essential while working on a project. It will be simple...

7 minutes read.

Types of polymorphism in C++

What is polymorphism in C++ ? Polymorphism literally translates to "multiple forms". This indicates that the same thing behaves differently depending on the context in programming. Polymorphism is a feature of C++...

6 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.