Division of two numbers in C++

Updated

In this article, we have discussed various methods to divide two numbers in C++. These are simple program, you can learn them easily.

Method-1 : With Hardcoded Inputs


#include <iostream>
using namespace std;
  
//driver
int main() {
  
   // first number is 4
   int x = 4;
  
   // second number is 2
   int y = 2;
  
   // resultant number
   int division = x / y;
   cout <<  division;
   return 0;
}

Output:

2 

Method-2 : With user Inputs

#include <iostream>
using namespace std;
  
// driver
int main() {
  
   // for inputs
   int x, y;
  
   // for output
   int  division;
  
   cout << "first number: ";
   cin >> x;
   cout << "second number: ";
   cin >> y;
     
    // division of both numbers
    division = x / y;
   cout << " division is: " <<  division;
   return 0;
}

Output:
Output depends on user inputs, that will be division of both numbers.

Method-3 : With class

#include <iostream>
using namespace std;
   
class Division {
    
  // for inputs
  int x, y;
   
public:
  // input numbers
  void input() {
    cout << "Input two integers\n";
    cin >> x >> y;
  }
  void performDivision() {
    //  division of both numbers
    cout << "Result: " << x / y << endl;
  }
};
   
// driver
int main()
{ 
   // object of class
    Division m; 
   
   m.input();
   m.performDivision();
   
   return 0;
}

Output:
Output depends on user inputs, that will be division of both numbers.

Method-4 : With array elements

#include <iostream>
using namespace std;
  
//driver
int main() {
  
   // create array
   int a[3];
  
   // initialize array 
   a[0]=4;
   a[1]=2;
  
   //  subtraction of first two elements
   a[2]=a[0] / a[1];
   cout<<" division of "<<a[0]<<" and "<<a[1]<<" is "<<a[2];
   return 0;
}

Output:

division of 4 and 2 is 2 

You can watch this good video about this topic:



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.

Comment
Next tutorial
RKplus Published 27 May 2020 · 2 min read
Topics:

Related tutorials

  • Call by value, call by address and call by reference in C++

    The parameters of the function call are known as actual parameters and the parameters of the function definition are known as formal parameters. There are 3 ways of…

    3 min read
  • Delete an element or a range from a set in C++ STL

    Delete an element from the Set by passing its value : Approach : In this method, an element is deleted by using the erase function and calling it…

    3 min read
  • Four Pillars of Object-Oriented Programming (OOPS)

    Get an overview of four pillars of object-oriented programming (OOP) and see their suitable real life examples. What is OOP? Picture a cluttered garage?️. Tools are scattered everywhere,…

    4 min read
  • What is Overriding in C++

    Write a program in C++ to depict the concept of overriding or Dynamic polymorphism. Overriding is a type of polymorphism. Polymorphism in OOP languages means to take more…

    2 min read
  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read