C# Math.DivRem Method

CsharpProgrammingServer Side Programming

Use the Math.DivRem method to calculate the quotient of two numbers and return the remainder.

Firstly, set dividend and divisor.

// dividend
long dividend = 30;
// divisor
long divisor = 7;

Now, use the DivRem method.

long quotient = Math.DivRem(dividend, divisor, out rem);

Example

 Live Demo

using System;
using System.Globalization;
class Demo {
   static void Main() {
      // remainder
      long rem;
      // dividend
      long dividend = 98;
      // divisor
      long divisor = 9;
      long quotient = Math.DivRem(dividend, divisor, out rem);
      Console.WriteLine("{0} \\ {1} = {2} and remainder = {3}", dividend, divisor, quotient, rem);
   }
}

Output

98 \ 9 = 10 and remainder = 8
raja
Published on 14-Sep-2018 17:22:05
Advertisements