×

Print Table Using 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 12, then we have to multiply 12 by each natural number as follows:

12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60.... 

What is while Loop?

A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the condition no longer met.

The syntax of the while loop

               while(condition)
                     {
                        statement(x);
                     }

Algorithm to print Multiplication of Table 2:

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 for-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 1:

#include<iostream>
using namespace std;
int main()
{
    int num=2, i, res;
   i=1; 
while(i<=12){
        res = num*i;
        cout<<num<<" * "<<i<<" = "<<res;
        cout<<endl;
i++;
    }
    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 while loop is used in this program. The value of i is initialized to 1, and the condition (i<12) is evaluates to true because (1<12), which allows the program to enter in while loop. And, res stores the value of num*i, i.e., 2*1, i.e., 2.

Now, use the following statement:
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.

The program flow moves to the update statement which increases the value of i. Consequently, i=2

The condition i<12 or 2<12 evaluates to true once more. Again, control enters in the loop.
This process will continue until the condition is determined to be untrue.
The multiplication table of 2 is printed on the output screen.

Program 2: Program to print a table of a number defined by user at run time:

#include<iostream>
using namespace std;
int main()
{
    int num, i, res;
    cout<<"Enter the Number: ";
    cin>>num;
    i=1;
    while(i<10){
        res = num*i;
        cout<<num<<" * "<<i<<" = "<<res;
        cout<<endl;
    i++;
}
    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

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 iterate 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 While Loop in C++

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;
while(i<=10)
    {
       j=1;
while(j<=n) 
        {
            if (j <= n - 1)
                cout<<j<<"x"<<i<<"="<<i*j<<"\t";
            else
                cout<<j<<"x"<<i<< "=" <<i*j<<endl;
        j++;
}
      i++;
      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

strcat() vs strncat() in C++

In this tutorial, we will explore about strcat() and strncat() in the most usable language C++. We will also look at the difference between them. strcat() C++ is a computer language with...

4 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input. Let us look at an example of a program in...

2 minutes read.

How to Handle Divide by Zero Exception in C++

If you are a programmer or interested in coding then it is obvious that you face some illogical test cases. Suppose, you have written one program that calculates the factorial...

6 minutes read.

How to run program in turbo c++

What is Turbo C++? Turbo c++ is an integrated development environment (IDE) and a compiler to run C++ code. Turbo c++ helps to link the header files with the main code....

2 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.

Returning a Function Pointer from a Function in C/C++

Pointers to functions can be used in the C programming language just like standard data pointers such as "int *," "char *," etc. The following is a basic example of a...

3 minutes read.

Binary Operator Overloading in C++

The Binary Operator Overloading in the C++ programming language will be covered in this part. An operator which comprises two operands to execute a mathematical operation is termed the Binary...

6 minutes read.

Two dimension array

C++ Two dimension (2D) Array Two dimension (2D) array is an array of arrays. It is represented in the form of row and column. The elements of 2D array are accessed through the...

2 minutes read.

Queue in C++

What is Queue? As the name suggests, the queue is the type of data structure that follows the FIFO (First In - First Out) mechanism. In simple words, it is...

4 minutes read.

C++ Inline function

A C++ function that extends in line when called is known as an inline function. It reduces function call overhead by having the compiler use the function code rather than...

4 minutes read.

Differences Between C Structures and C++ Structures

The below table clearly describes the differences between C and C++ programming languages Structures concepts where the core principle concepts like static members, data handling, pointers, access modifiers, the importance...

3 minutes read.

Scope Resolution Operator in C++

The scope resolution operator and its different usage in the C++ programming language will be discussed in this section. The scope resolution operator is used to refer to an out-of-scope...

6 minutes read.

C++ Mutable keyword

C++ mutable keyword Mutable class storage specifier in C++ Storage class specifiers in C are auto, register, static, and external. Typedef is also considered as a specifier of the storage...

4 minutes read.

How to Declare Unordered Sets in C++

The implementation of an unordered set using a hash table ensures that the insertion is always randomised by hashing the keys into hash table indices. When we define keys of...

4 minutes read.

User-defined literals in C++

Introduction to User-Defined Literals: User-defined literals, introduced in C++11, are a way to extend the C++ language to allow users to define their literal suffixes and the corresponding behavior. These suffixes...

7 minutes read.

C++ If-else-if

Introduction: If-else-if control statement is an if statement used with an optional else if control statement to check multiple conditions. In this control statement, when any one of the condition returns...

4 minutes read.

C++ STL (Standard Template Library)

Introduction C++ is a flexible type and general proposed programming language. So we need a standard library that supports C++. C++ STL (Standard Template Library) is a collection of templates that...

6 minutes read.

Array of Object in C++

What is an Array? In C++, an array is a group of related data types, such as int, char, float, double, etc., that are easily accessed by index value alone and...

12 minutes read.

Difference between "int main( ) and int main(void)" in C/C++

int main( ) function In the C/C++ programming language, int main() indicates a function that returns an integer at the end of the program execution. In general, a value of '0' indicates...

3 minutes read.