C library function - div()
Advertisements
Description
The C library function div_t div(int numer, int denom) divides numer (numerator) by denom (denominator).
Declaration
Following is the declaration for div() function.
div_t div(int numer, int denom)
Parameters
numer -- This is the numerator.
denom -- This is the denominator.
Return Value
This function returns the value in a structure defined in <cstdlib>, which has two members. For div_t:int quot; int rem;
Example
The following example shows the usage of div() function.
#include <stdio.h>
#include <stdlib.h>
int main()
{
div_t output;
output = div(27, 4);
printf("Quotient part of (27/ 4) = %d\n", output.quot);
printf("Remainder part of (27/4) = %d\n", output.rem);
output = div(27, 3);
printf("Quotient part of (27/ 3) = %d\n", output.quot);
printf("Remainder part of (27/3) = %d\n", output.rem);
return(0);
}
Let us compile and run the above program, this will produce the following result:
Quotient part of (27/ 4) = 6 Remainder part of (27/4) = 3 Quotient part of (27/ 3) = 9 Remainder part of (27/3) = 0