### Using Math.DivRem function to get Quotient and Remainder in Asp.Net C#,VB

Introduction:  In this article I have explained how to compute/ calculate quotient and remainder after division operation using Math.DivRem inbuilt method and other techniques in Asp.Net with both C# and VB language.

Implementation: Below I have mentioned three ways to get the quotient and remainder from division operation.

Asp.Net C# Code

First Way: Using (/) Divide and %( Modulus operator)

int quotient, remainder;
quotient = 17 / 5;
remainder = 17 % 5;
Response.Write("Quotient: " + quotient + " Remainder: " + remainder);

Result: Quotient: 3 Remainder: 2

Second Way: Using (/) Divide,  -( Minus)  and *(Multiply) Operators

int quotient, remainder;
quotient = 17 / 5;
remainder = 17 - (5 * quotient);
Response.Write("Quotient: " + quotient + " Remainder: " + remainder);

Result: Quotient: 3 Remainder: 2

Third Way: Using Math.DivRem

int quotient, remainder;
quotient = Math.DivRem(17, 5, out remainder);
Response.Write("Quotient: " + quotient + " Remainder: " + remainder);

Result: Quotient: 3 Remainder: 2

Explanation : Math.Div function calculates the quotient of two numbers and also returns the remainder in an output parameter. So using Math.DivRem we get the quotient and the remainder both in two separate variables.
Here in our example 17 is Dividend, 5 is Divisor so on division we should get 3 as a quotient and 2 as a remainder which we have got through Math.Div function.

Asp.Net VB Code

First Way: Using (/) Divide and %( Modulus operator)

Dim quotient As Integer, remainder As Integer
quotient = 17 / 5
remainder = 17 Mod 5
Response.Write("Quotient: " + quotient + " Remainder: " + remainder)

Result: Quotient: 3 Remainder: 2

Second Way: Using (/) Divide,  -( Minus)  and *(Multiply) Operators

Dim quotient As Integer, remainder As Integer
quotient = 17 / 5
remainder = 17 - (5 * quotient)
Response.Write("Quotient: " + quotient + " Remainder: " + remainder)

Result: Quotient: 3 Remainder: 2

Third Way: Using Math.DivRem

Dim quotient As Integer, remainder As Integer
quotient = Math.DivRem(17, 5, remainder)
Response.Write("Quotient: " + quotient + " Remainder: " + remainder)
Result: Quotient: 3 Remainder: 2

Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.
Previous
Next Post » 