div() function in C++


The C / C++ library function div_t div(int numer, int denom) divides numer (numerator) by denom (denominator). Following is the declaration for div() function.

div_t div(int numer, int denom)

The parameters are numerator and the denominator. This function returns the value in a structure defined in <cstdlib>, which has two members. For div_t:int quot; int rem;

Example

 Live Demo

#include <iostream>
#include <cstdlib>
using namespace std;
int main () {
   div_t output;
   output = div(27, 4);
   cout << "Quotient part of (27/ 4) = " << output.quot << endl;
   cout << "Remainder part of (27/4) = " << output.rem << endl;
   output = div(27, 3);
   cout << "Quotient part of (27/ 3) = " << output.quot << endl;
   cout << "Remainder part of (27/3) = " << output.rem << endl;
   return(0);
}

Output

Quotient part of (27/ 4) = 6
Remainder part of (27/4) = 3
Quotient part of (27/ 3) = 9
Remainder part of (27/3) = 0

Updated on: 30-Jul-2019

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements