lldiv() Function

The lldiv() function is declared in the header file <stdlib.h>.

The lldiv() function performs integral division and returns both the quotient and the remainder in a single structure. This function simplifies the process of obtaining the result of a division operation by bundling the two values together.


Syntax of lldiv()

</>
Copy
lldiv_t lldiv(long long int numer, long long int denom);

Parameters

ParameterDescription
numerThe numerator for the division operation.
denomThe denominator for the division operation.

The function computes both the quotient and the remainder in one call. The result is stored in a structure of type lldiv_t that contains two members: quot for the quotient and rem for the remainder.


Return Value

The function returns a structure of type lldiv_t by value. This structure contains the quotient in the member quot and the remainder in the member rem of the division operation.


Examples for lldiv()

Example 1: Basic Division with Positive Numbers

This example demonstrates the use of lldiv() to divide two positive numbers and display the quotient and remainder.

Program

</>
Copy
#include <stdio.h>
#include <stdlib.h>

int main() {
    long long int numerator = 12345;
    long long int denominator = 100;
    lldiv_t result = lldiv(numerator, denominator);

    printf("Numerator: %lld, Denominator: %lld\n", numerator, denominator);
    printf("Quotient: %lld\n", result.quot);
    printf("Remainder: %lld\n", result.rem);

    return 0;
}

Explanation:

  1. Two long long integers are defined for the numerator and denominator.
  2. lldiv() is called with these values, returning a structure with the quotient and remainder.
  3. The results are printed to the console.

Program Output:

Numerator: 12345, Denominator: 100
Quotient: 123
Remainder: 45

Example 2: Division Involving Negative Numbers

This example shows how lldiv() handles division when the numerator or denominator is negative.

Program

</>
Copy
#include <stdio.h>
#include <stdlib.h>

int main() {
    long long int numerator = -98765;
    long long int denominator = 123;
    lldiv_t result = lldiv(numerator, denominator);

    printf("Numerator: %lld, Denominator: %lld\n", numerator, denominator);
    printf("Quotient: %lld\n", result.quot);
    printf("Remainder: %lld\n", result.rem);

    return 0;
}

Explanation:

  1. Negative values are used for the numerator while the denominator remains positive.
  2. The lldiv() function calculates the quotient and remainder according to the rules of integer division with negatives.
  3. The output reflects the correct signed quotient and remainder.

Program Output:

Numerator: -98765, Denominator: 123
Quotient: -802
Remainder: -119

Example 3: Using lldiv() in a Calculation Loop

This example illustrates how lldiv() can be used repeatedly within a loop to process a series of division operations.

Program

</>
Copy
#include <stdio.h>
#include <stdlib.h>

int main() {
    long long int numerators[] = {1000, 2000, 3000};
    long long int denominator = 250;
    int n = sizeof(numerators) / sizeof(numerators[0]);

    for (int i = 0; i < n; i++) {
        lldiv_t result = lldiv(numerators[i], denominator);
        printf("For Numerator: %lld, Denom: %lld => Quotient: %lld, Remainder: %lld\n", 
                numerators[i], denominator, result.quot, result.rem);
    }

    return 0;
}

Explanation:

  1. An array of numerators is defined along with a common denominator.
  2. A loop iterates over the array, applying lldiv() to each numerator.
  3. The quotient and remainder for each division are printed out.

Program Output:

For Numerator: 1000, Denom: 250 => Quotient: 4, Remainder: 0
For Numerator: 2000, Denom: 250 => Quotient: 8, Remainder: 0
For Numerator: 3000, Denom: 250 => Quotient: 12, Remainder: 0