Tuesday, October 19, 2010

Math.DivRem Function

In this short post I will show a simple and helpful function for calculating the Quotient and the Reminder when doing math calculation.The System.Math static class contain the function with the name DivRem which is used to get the quotient and the remainder when dividing one value to the second value.Math.DivRem calculate the quotient of two 32-bit signed integers and also returns the remainder in an output parameter.

int intQuotient = 0; int intRemainder = 0; intQuotient= Math.DivRem(19, 5, out intRemainder); Console.WriteLine("Reminder: {0} and Quotient : {1}", intRemainder, intQuotient); Console.ReadKey();

The output of the above code will be, Here you can see that remainder is 4 and quotient is 3 when dividing 19 with 5.

Reminder: 4 and Quotient :3

The parameter which are passed to the Math.DivRem are.
1- The first parameter will be dividend.
2- The second parameter will be Divisor.
3- The third parameter will be the remainder as output.

The Math.DivRem method performs an essential function for certain mathematical programs. If you need to calculate the remainder and store it after a division operation, use the Math.DivRem method instead of the division (/) or modulo (%) operators.


All and any comments / bugs / suggestions are welcomed!

No comments: