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 »

1 comments:

Click here for comments
for ict 99
admin
January 23, 2018 ×

great

Congrats bro for ict 99 you got PERTAMAX...! hehehehe...
Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..