C++ Standard Library C++ STL Library

C++ <cstdlib> - ldiv() Function



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

Syntax

ldiv_t ldiv (long int numer, 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 ldiv_t defined as follows:

struct ldiv_t { long quot; long rem; };

or 

struct ldiv_t { long rem; long quot; };

Example:

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

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

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

The output of the above code will be:

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

❮ C++ <cstdlib> Library