Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Perform division of two numbers in C language|using 6ways

Divide two numbers in C++ language|6ways

Posted on September 7, 2020September 9, 2020

Table of Contents

  • Divide two numbers in C++ language|6ways
      • Divide two numbers in C++ -using standard method
      • Divide two numbers in C++ -using user input
      • Divide two numbers in C++ -using function
      • Divide two numbers in C++ -using recursion
      • Divide two numbers in C++ -using without divisional operator
      • Divide two numbers in C++ -using pointer

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

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes