×

Print Table Using For-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 For Loop?

A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently.

The syntax that can be used for the for loop in the C++ programming language is given below:

for (initialization statement; termination condition; increment or decrement statement (used for modifying value for further evaluation))
{
    /* main body of the “for” loop */
}

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;
    for(i=1; i<=12; i++)
    {
        res = num*i;
        cout<<num<<" * "<<i<<" = "<<res;
        cout<<endl;
    }
    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 for 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.

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 any user-defined number

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

Related Topics

C++ Socket Programming

In this world, computer networking has become very important for sharing of data. Every good programmer has some knowledge about computer networking. Socket programming is one of the critical topics...

6 minutes read.

While Loop Examples in C++

While Loop A while loop repeats all code in its body, also known as a while statement, as long as a specific condition is satisfied. The loop ends if or when...

4 minutes read.

How to create a library in C++

Before going on to the creation of a library, let’s understand its meaning. What is a library? In simple words, a library is a collection of numerous functions, methods, classes, header files...

7 minutes read.

Pthreads or POSIX Threads in C++

The thread API for C/C++ is implemented by pthreads or POSIX threads. It enables the multithreading system, which enables parallel and distributed processing, and the creation of new concurrent process...

3 minutes read.

C++ Forward Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 minutes read.

10 Best C and C++ Books for Beginners & Advanced Programmers

If you want to become a skilled software developer, you should never stop learning, whether you're a working professional or a student. Why, therefore, only C or C++? The fundamental...

6 minutes read.

Unary Operators in C++

Unary operators in C++ Unary operator: is operations that function to produce a new value on a single operand. a) unary minus: A minus operator modifies the argument's symbol. A positive number...

3 minutes read.

Find the Size of Array in C/C++ without using sizeof() function

We know that arrays in C/C++ are the most essential data structures as they have the ability to hold the data in a continuous manner line where the address of...

3 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

Accumulate() and partial_sum() in C++ STL Numeric header

The C++ STL's numeric library includes the numeric header. This library provides efficient numeric arrays, support for random number generation, and fundamental mathematical operations and types. Several of the numeric...

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

C++ Ternary Operator

In this tutorial, we'll learn about the C++ ternary operator and how to utilise it to manage the program's flow using examples. Ternary Operator: The if-else statement and the conditional operator use...

3 minutes read.

swap() function in C++

Swap() function: swap() function in C++: swap() function is a pre-define function in c++ present in STL( Standard template library ). It is used to swap two numbers. It takes two mandatory...

6 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.

Factory Method for Designing Pattern in C++

In C++, the factory method is a type of conditional design pattern. The factory method is related to creating a new object in C++. With the help of a factory...

3 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

Abstract class in C++

In this article, you will get exposure to an abstract class in C++. We will discuss this topic using some practical examples too. To understand the abstract classes, you should...

6 minutes read.

How to enter a name in C++

A name is a string or array of characters or letters. The string is one of the most helpful data types offered by the C++ library. A string helps the...

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.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.