C++

Multiplication table program of a number in given range using C++

Multiplication table program of a number in given range using C++

In this article, we will discuss the concept of Multiplication table program of a number in given range using C++

In this program, we are going to learn how to generate a multiplication table using many ways in C++ language.

This is done using for loop , while loop , do-while loop and method in C++ language

 

Multiplication chart of a number in given range using C++- for loop

In this code, we will display multiplication table of a number in given range using for loop in C++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,num,range;
    cout<<"Enter the any number\n";
    cin>>num;
      cout<<"Enter the range\n";
    cin>>range;
    
    for(i=1; i<=range; i++){
        cout << num <<" * " << i<< " = "<<num*i<<endl;
         
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter the any number
12
Enter the range
8

12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96

 

  • integer variable num and range are declared.
  • The program asks input from the user
  • Then the user enters the input values for num amd range.
  • The program will read the input using cin>> and store to the variable num and range respectively
  • Create a for loop of i from 1 to 8 and increase the value of i after every iteration by 1
  • finally, the program displays the multiplication table using cout<<.

 

Multiplication chart of a number in given range using C++- while loop

In this code, we will display multiplication table of a number in given range using while loop in C++ language

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,num,range;
    cout<<"Enter the any number\n";
    cin>>num;
      cout<<"Enter the range\n";
    cin>>range;
    i=1;
    while(i<=range){
        cout << num <<" * " << i<< " = "<<num*i<<endl;
         i++;
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter the any number
15
Enter the range
9

15 x 1 = 15
15 x 2 = 30
15 x 3 = 45
15 x 4 = 60
15 x 5 = 75
15 x 6 = 90
15 x 7 = 105
15 x 8 = 120
15 x 9 = 135

 

 

  • integer variable num and range are declared.
  • The program asks input from the user
  • Then the user enters the input values for num amd range.
  • The program will read the input using cin>> and store to the variable num and range respectively
  • Create a while loop of i from 1 to 9 and increase the value of i after every iteration by 1
  • finally, the program displays the multiplication table using cout<<.

 

Multiplication chart of a number in given range using C++- do-while loop

In this code, we will display multiplication table of a number in given range using do-while loop in C++ language

Program 3

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,num,range;
    cout<<"Enter the any number\n";
    cin>>num;
      cout<<"Enter the range\n";
    cin>>range;
    i=1;
    do{
        cout << num <<" * " << i<< " = "<<num*i<<endl;
         i++;
    }while(i<=range);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter the any number
5
Enter the range
12

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
5 x 11= 55
5 x 12= 60

 

 

  • integer variable num and range are declared.
  • The program asks input from the user
  • Then the user enters the input values for num amd range.
  • The program will read the input using cin>> and store to the variable num and range respectively
  • Create a do-while loop of i from 1 to 12 and increase the value of i after every iteration by 1
  • finally, the program displays the multiplication table using cout<<.

 

Multiplication chart of a number in given range using C++- function

In this code, we will display multiplication table of a number in given range using function in C++ language

Program 3

#include <iostream>
#include <conio.h>
using namespace std;

void mutipication_Table(int num, int range){
int counter;
for(counter=1; counter<=range; counter++){
    cout<<num<<"x"<<counter<<"="<<num*counter<<endl;
}
}
int main()
{
    int num,range;
    cout<<"Enter a number: \n";
    cin>>num;
     cout<<"Enter the range: \n";
    cin>>range;
    mutipication_Table(num,range);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter the any number
12
Enter the range
4
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48

 

 

  • integer variables num and range is declared.
  • The program asks input from the user
  • Then the user enters the input values for num and range.
  • The program will read the input using cin>> and store the variable num range respectively.
  • Define the function  multiplication_Table(int) to print multiplication table
  • Call the function to produce output
  • finally, the program displays the multiplication table using cout<<.

 

Suggested post

For loop in C++ language

While loop in C++ language

Do-While loop in C++ language

Function in C++ language

Recursion in C++ language

 

Similar post

Code to Display multiplication table in Java

Code to Display multiplication table in C

Code to Display multiplication table in C++

Code to Display multiplication table in Python

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

9 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

10 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

10 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

10 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

10 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

10 months ago

This website uses cookies.