C++ lldiv()

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

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

It is defined in <cstdlib> header file.

Mathematically,

quot * y + rem = x

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

lldiv_t lldiv(long long int x, long long int y);
lldiv_t lldiv(long long x, long long y);

The lldiv() 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.


lldiv() Parameters

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

lldiv() Return value

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

struct lldiv_t {
	long long quot;
	long long rem;
};

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

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

int main()
{
	long long nume = 998102910012LL;
	long long deno = 415LL;
	
	lldiv_t result = lldiv(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 998102910012/415 = 2405067253
Remainder of 998102910012/415 = 17
Did you find this article helpful?