C ldiv() function
C ldiv() function - Long and Long Long Division
Syntax ldiv() function
div_t div(long int numer, long int denom)
The C library function div_t div(long int numer, long int denom) divides numer (numerator) by denom (denominator).
The ldiv() function is used to calculate the quotient and remainder of the division of numerator by denominator.
Parameters ldiv() function
Name | Description | Required /Optional |
---|---|---|
numer | This is the numerator. | Required |
denom | This is the denominator. | Required |
Return value from ldiv()
- A structure of type ldiv_t, containing both the quotient (long int quot) and the remainder (long int rem).
- If the value cannot be represented, the return value is undefined.
- If denominator is 0, an exception is raised.
Example: ldiv() function
The following example shows the usage of ldiv() function.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
long int nums[4] = {7,76,-45,-34};
long int den[4] = {3,7,5,-7};
ldiv_t ans; /* ldiv_t is a struct type containing two long ints:
'quot' stores quotient; 'rem' stores remainder */
short i,j;
printf("Results of long division:\n\n");
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
{
ans = ldiv(nums[i], den[j]);
printf("Dividend: %6ld Divisor: %6ld", nums[i], den[j]);
printf(" Quotient: %6ld Remainder: %6ld\n", ans.quot, ans.rem);
}
}
Output:
Results of long division: Dividend: 7 Divisor: 3 Quotient: 2 Remainder: 1 Dividend: 7 Divisor: 7 Quotient: 1 Remainder: 0 Dividend: 7 Divisor: 5 Quotient: 1 Remainder: 2 Dividend: 7 Divisor: -7 Quotient: -1 Remainder: 0 Dividend: 76 Divisor: 3 Quotient: 25 Remainder: 1 Dividend: 76 Divisor: 7 Quotient: 10 Remainder: 6 Dividend: 76 Divisor: 5 Quotient: 15 Remainder: 1 Dividend: 76 Divisor: -7 Quotient: -10 Remainder: 6 Dividend: -45 Divisor: 3 Quotient: -15 Remainder: 0 Dividend: -45 Divisor: 7 Quotient: -6 Remainder: -3 Dividend: -45 Divisor: 5 Quotient: -9 Remainder: 0 Dividend: -45 Divisor: -7 Quotient: 6 Remainder: -3 Dividend: -34 Divisor: 3 Quotient: -11 Remainder: -1 Dividend: -34 Divisor: 7 Quotient: -4 Remainder: -6 Dividend: -34 Divisor: 5 Quotient: -6 Remainder: -4 Dividend: -34 Divisor: -7 Quotient: 4 Remainder: -6
C Programming Code Editor:
Previous C Programming: C labs()
Next C Programming: C rand()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming/stdlib/c-ldiv.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics