Possible Duplicate:
Divide and Get Remainder at the same time?
Is it possible to get both the quotient and the remainder of integer division in a single step, i.e., without performing integer division twice?
When we divide A by B in long division, Q is the quotient and R is the remainder.
When we divide a positive integer (the dividend) by another positive integer (the divisor), we obtain a quotient. We multiply the quotient to the divisor, and subtract the product from the dividend to obtain the remainder. Such a division produces two results: a quotient and a remainder.
The Math. DivRem() method in C# is used to divide and calculate the quotient of two numbers and also returns the remainder in an output parameter.
Quotient = Dividend ÷ Divisor. Let us solve 435 ÷ 4. Here, 435 is the dividend and 4 is the divisor.
div
will do this. See reference and example:
/* div example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
div_t divresult;
divresult = div (38,5);
printf ("38 div 5 => %d, remainder %d.\n", divresult.quot, divresult.rem);
return 0;
}
Output:
38 div 5 => 7, remainder 3.
EDIT:
The C Specification says:
The types declared are size_t and wchar_t (both described in 7.17),
div_t
which is a structure type that is the type of the value returned by the div function,
ldiv_t
which is a structure type that is the type of the value returned by the ldiv function, and
lldiv_t
which is a structure type that is the type of the value returned by the lldiv function.
... but it doesn't say what the definition of div_t
is.
Yes, there is a standard function called div()
(and ldiv
, and maybe even lldiv
) that does this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With