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()
ldiv_t ldiv(long int numer, long int denom);
Parameters
Parameter | Description |
---|---|
numer | The numerator (dividend) for the division. |
denom | The 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
#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:
- The numerator (100) and denominator (7) are defined as long integers.
- The
ldiv()
function is called to perform the division. - 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
#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:
- A negative numerator (-50) and a positive denominator (8) are used.
- The
ldiv()
function computes the quotient and remainder according to the rules of integer division. - 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
#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:
- The numerator (56) and denominator (7) are chosen such that the division has no remainder.
- The
ldiv()
function returns a quotient of 8 and a remainder of 0. - 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
#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:
- An array of numbers is defined to be divided by a common divisor (25).
- A loop iterates over each number in the array.
- 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