Arithmetic Caculation

Divide two numbers in C++ language|6ways

Divide two numbers in C++ language|6ways

In this tutorial, we will discuss the Divide two numbers in C++ language

In this post, we are going to learn how to find division of two numbers using different 6 ways in C++ language

Divide two numbers in C++ -using standard method

Program 1

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

int main()
{
    int num1=100,num2=20;
    int div=num1/num2;
    cout << "Division of "<<num1<<" and "<<num2<<" is: " << div<<endl;
    getch();
    return 0;
}

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

Division of 100 and 20 is: 5

 

In this program,

  • Integer variable num1 and num2 both are declared and initialized
  • The num1 and num2 both are divided together and the value assigned to the integer variable div
  • Finally the program is displayed the output using cout<<.

 

Divide two numbers in C++ -using user input

Program 2

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

int main()
{
    int num1,num2;
    cout<<"Enter two numbers: ";
    cin>>num1>>num2;
    int div=num1/num2;
    cout << "Division of "<<num1<<" and "<<num2<<" is: " << div<<endl;
    getch();
    return 0;
}

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

Enter two numbers: 666
6
Division of 666 and 6 is: 111

 

in tis program,

  • Integer variable num1 and num2 both are declared,
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • The program will read the input using cin>> and store the variables  num1 and num2.
  • The num1 and num2 both are divided together and the value assigned to the integer variable div
  • Finally the program is displayed the output using cout<<

 

Divide two numbers in C++ -using function

Program 3

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

int division(int,int);   //function prototype / declaration
int main()
{
    int num1,num2,result;
    cout<<"Enter two number to find division\n";
    cin>>num1>>num2;  //numbers receive from the user
    result=division(num1,num2);//assign the output to variable result
    //function call
     cout << "Division of "<<num1<<" and "<<num2<<" is: " << result<<endl;
    getch();
    return 0;
}

int division(int a, int b)
{

        return a/b;
    }

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

Enter two numbers:
560
70
Division of 560 and 70 is: 8

 

In this program

  • Declare a function named as division() with two int parameters
  • Declare num1,num2 and result.
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • The program will read the input using cin>> and store the variables in num1, num2 respectively
  • Define the function (division())for find division
  • Call the function to produce output
  • Finally, the program displays the result of division using cout<<

 

 

Divide two numbers in C++ -using recursion

Program 4

#include <iostream>
#include <conio.h>
using namespace std;
int division(int,int);   //function prototype / declaration
int main()
{
    int num1,num2,result;
    cout<<"Enter two number to find division\n";
    cin>>num1>>num2;  //numbers receive from the user
    result=division(num1,num2);//assign the output to variable result
    //function call
     cout << "Division of "<<num1<<" and "<<num2<<" is: " << result<<endl;
    getch();
    return 0;
}


int division(int x, int y)
{
    if (y==0)
{
  return 0;
}
else if (x-y==0){
  return 1;
}
else if (x<y){
  return 0;
}
else{
  return (1+division(x-y,y));
}
}

 

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

Enter two numbers to find division:
448
16
Division of 448 and 16 is: 28

 

  • Declare a function named as division() with two int parameters
  • Declare num1,num2 and result.
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • The program will read the input using cin>> and store the variables in num1, num2 respectively
  • Define the function (division())for find division recursively
  • Call the recursive function to produce output
  • Finally, the program displays the result of subtraction using cout<<

 

Divide two numbers in C++ -using without divisional operator

Program 5

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


int division(int,int);   //function prototype / declaration
int main()
{
    int num1,num2,a,b,counter=0;
    cout<<"Enter two number to find division\n";
    cin>>a>>b;  //numbers receive from the user

   num1=a;
   num2=b;
   while(num1>=num2)
   {
       num1=num1-num2;
       counter++;
   }

     cout << "Division of "<<a<<" and "<<num2<<" is: " << counter<<endl;
    getch();
    return 0;
}

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

Enter two numbers to find division:
1250
25
Division of 560 and 70 is:50

 

Divide two numbers in C++ -using pointer

Program 6

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

int main()
{
    int num1,num2,*ptr1,*ptr2;
    //variable declaration

    cout << "Enter the first number: " << endl;
    cin>>num1;//read the value for num1
     cout << "Enter the second number: " << endl;
    cin>>num2;//read the value for num2

    ptr1=&num1;
    ptr2=&num2;

    int div = *ptr1/ *ptr2;//division calculation
      cout <<num1 <<" is devided by "<< num2<<" is: "<<div<<endl;
getch();
    return 0;
}

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

Enter the first  number:
1250
Enter the second number
25
1250 is divided by 25 is: 50

 

  • Declare variables num1,num2 and div.
  • Declare pointer variables *ptr1, *ptr2.
  • The program takes input from the user
  • Then the user enters the input value for num1 and num2
  • The program will read the input using cin>> and store the variables in num1, num2 respectively
  • Address of the variables num1 and num2 assigned to pointer variables ptr1 and ptr2 respectively
  • calculate the division of the numbers using pointer variable  and Assign the product value to div variable
  • Finally, the program displays the result using cout<<

 

Suggested for you

Data type in C++ language

Variable in C++ language

Operator in C++ language

While loop in C++ language

 

Similar post

C program to calculate division of two numbers | 6 different Methods

Java program to calculate division of two numbers | 5 different Methods

Python program to calculate division of two numbers | 4 different Methods

 

Karmehavannan

Recent Posts

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,…

11 months 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…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

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

11 months 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…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.