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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.