C div() function
C div() function - Calculate quotient and remainder
The div() function is used to calculate the quotient and remainder of the division of numerator by denominator.
Syntax div() function
div_t div(int numer, int denom)
Parameters div() function
Name | Description | Required /Optional |
---|---|---|
numer | The numerator. | Required |
denom | The denominator. | Required |
Return value from div() function
- A structure of type div_t, containing both the quotient int quot and the remainder integer rem.
- If the return value cannot be represented, its value is undefined.
- If denominator is 0, an exception will be raised.
Example: div() function
The following example shows the usage of div() function.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int nums[4] = {6,56,-34,-45};
int den[4] = {5,7,3,-7};
div_t ans; /* div_t is a struct type contain two ints:
'quot' stores quotient; 'rem' stores remainder*/
short i,j;
printf("Results of division:\n");
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
{
ans = div(nums[i],den[j]);
printf("Dividend: %6d Divisor: %6d", nums[i], den[j]);
printf(" Quotient: %6d Remainder: %6d\n", ans.quot, ans.rem);
}
}
Output:
Results of division: Dividend: 6 Divisor: 5 Quotient: 1 Remainder: 1 Dividend: 6 Divisor: 7 Quotient: 0 Remainder: 6 Dividend: 6 Divisor: 3 Quotient: 2 Remainder: 0 Dividend: 6 Divisor: -7 Quotient: 0 Remainder: 6 Dividend: 56 Divisor: 5 Quotient: 11 Remainder: 1 Dividend: 56 Divisor: 7 Quotient: 8 Remainder: 0 Dividend: 56 Divisor: 3 Quotient: 18 Remainder: 2 Dividend: 56 Divisor: -7 Quotient: -8 Remainder: 0 Dividend: -34 Divisor: 5 Quotient: -6 Remainder: -4 Dividend: -34 Divisor: 7 Quotient: -4 Remainder: -6 Dividend: -34 Divisor: 3 Quotient: -11 Remainder: -1 Dividend: -34 Divisor: -7 Quotient: 4 Remainder: -6 Dividend: -45 Divisor: 5 Quotient: -9 Remainder: 0 Dividend: -45 Divisor: 7 Quotient: -6 Remainder: -3 Dividend: -45 Divisor: 3 Quotient: -15 Remainder: 0 Dividend: -45 Divisor: -7 Quotient: 6 Remainder: -3
C Programming Code Editor:
Previous C Programming: C abs()
Next C Programming: C labs()
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-div.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics