C++ Standard Library C++ STL Library

C++ <cstdlib> - div() Function



The C++ <cstdlib> div() function computes both the quotient and the remainder of the division of the integral numerator numer by the integral denominator denom.

Syntax

div_t div (int numer, int denom);
ldiv_t div (long int numer, long int denom);
div_t div (int numer, int denom);
ldiv_t div (long int numer, long int denom);
lldiv_t div (long long int numer, long long int denom);

Parameters

numer Specify an integral value for numerator.
denom Specify an integral value for denominator.

Return Value

Returns both the remainder and the quotient can be represented as objects of type div_t defined as follows:

struct div_t { int quot; int rem; };

or 

struct div_t { int rem; int quot; };

Example:

The example below shows the usage of <cstdlib> div() function.

#include <iostream>
#include <cstdlib>
using namespace std;
 
int main (){
  div_t result = div(50, 17);

  cout<<"div(50, 17) gives quotient = "<<
      result.quot<<" and remainder = "<< 
      result.rem<<"\n";
  
  return 0;
}

The output of the above code will be:

div(50, 17) gives quotient = 2 and remainder = 16

❮ C++ <cstdlib> Library