ldiv() Function

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

The ldiv() function performs integral division of two long integers, providing both the quotient and the remainder in a single structure. This function is particularly useful when you need to obtain both results from a division operation without performing the division twice.


Syntax of ldiv()

</>
Copy
ldiv_t ldiv(long int numer, long int denom);

Parameters

ParameterDescription
numerThe numerator (dividend) for the division.
denomThe denominator (divisor) for the division.

This function calculates the quotient and remainder of the division of numer by denom in one operation. The results are stored in a structure of type ldiv_t, which contains two members: quot for the quotient and rem for the remainder.


Return Value

The function returns a structure of type ldiv_t by value. This structure contains two members: quot, which holds the integral quotient, and rem, which holds the remainder of the division.


Examples for ldiv()

Example 1: Basic Integral Division

This example demonstrates the basic usage of ldiv() to perform division and print the quotient and remainder.

Program

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

int main() {
    long int numerator = 100;
    long int denominator = 7;
    ldiv_t result = ldiv(numerator, denominator);

    printf("Division of %ld by %ld:\n", numerator, denominator);
    printf("Quotient: %ld\n", result.quot);
    printf("Remainder: %ld\n", result.rem);
    return 0;
}

Explanation:

  1. The numerator (100) and denominator (7) are defined as long integers.
  2. The ldiv() function is called to perform the division.
  3. The quotient and remainder are stored in the ldiv_t structure and then printed.

Program Output:

Division of 100 by 7:
Quotient: 14
Remainder: 2

Example 2: Division Involving Negative Numbers

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

Program

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

int main() {
    long int numerator = -50;
    long int denominator = 8;
    ldiv_t result = ldiv(numerator, denominator);

    printf("Division of %ld by %ld:\n", numerator, denominator);
    printf("Quotient: %ld\n", result.quot);
    printf("Remainder: %ld\n", result.rem);
    return 0;
}

Explanation:

  1. A negative numerator (-50) and a positive denominator (8) are used.
  2. The ldiv() function computes the quotient and remainder according to the rules of integer division.
  3. The result is printed, showing how the function handles negative numbers.

Program Output:

Division of -50 by 8:
Quotient: -6
Remainder: -2

Example 3: Division with Zero Remainder

This example demonstrates a division where the numerator is exactly divisible by the denominator, resulting in a zero remainder.

Program

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

int main() {
    long int numerator = 56;
    long int denominator = 7;
    ldiv_t result = ldiv(numerator, denominator);

    printf("Division of %ld by %ld:\n", numerator, denominator);
    printf("Quotient: %ld\n", result.quot);
    printf("Remainder: %ld\n", result.rem);
    return 0;
}

Explanation:

  1. The numerator (56) and denominator (7) are chosen such that the division has no remainder.
  2. The ldiv() function returns a quotient of 8 and a remainder of 0.
  3. The program prints the results, confirming the exact division.

Program Output:

Division of 56 by 7:
Quotient: 8
Remainder: 0

Example 4: Using ldiv() in a Loop for Multiple Divisions

This example illustrates how ldiv() can be used within a loop to perform a series of divisions and display their results.

Program

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

int main() {
    long int numbers[] = {100, 250, 375};
    long int divisor = 25;
    int size = sizeof(numbers) / sizeof(numbers[0]);

    for (int i = 0; i < size; i++) {
        ldiv_t result = ldiv(numbers[i], divisor);
        printf("Division of %ld by %ld: Quotient = %ld, Remainder = %ld\n",
               numbers[i], divisor, result.quot, result.rem);
    }
    return 0;
}

Explanation:

  1. An array of numbers is defined to be divided by a common divisor (25).
  2. A loop iterates over each number in the array.
  3. For each iteration, ldiv() is called to compute the quotient and remainder, which are then printed.

Program Output:

Division of 100 by 25: Quotient = 4, Remainder = 0
Division of 250 by 25: Quotient = 10, Remainder = 0
Division of 375 by 25: Quotient = 15, Remainder = 0