Join our newsletter for the latest updates.

C++ ldiv()

The ldiv() function in C++ computes the integral quotient and remainder of the division of two numbers.

The ldiv() can be thought as long int version of div().

It is defined in <cstdlib> header file.


quot * y + rem = x

ldiv() prototype [As of C++ 11 standard]

ldiv_t ldiv(long int x, long int y);
ldiv_t ldiv(long x, long y);

The ldiv() function takes a two arguments x and y, and returns the integral quotient and remainder of the division of x by y.

The quotient quot is the result of the expression x/y. The remainder rem is the result of the expression x%y.

ldiv() Parameters

  • x: Represents the numerator.
  • y: Represents the denominator.

ldiv() Return value

The ldiv() function returns a structure of type ldiv_t which consists of two members: quot and rem. It is defined as follows:

struct ldiv_t {
	long quot;
	long rem;

Example: How ldiv() function works in C++?

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
	long nume = 999810291L;
	long deno = 120L;

	ldiv_t result = ldiv(nume, deno);

	cout << "Quotient of " << nume << "/" << deno << " = " << result.quot << endl;
	cout << "Remainder of " << nume << "/" << deno << " = " << result.rem << endl;
	return 0;

When you run the program, the output will be:

Quotient of 999810291/120 = 8331752
Remainder of 999810291/120 = 51
Did you find this article helpful?