What is Math.DivRem() in C#?

C# has a built-in Math class that provides useful mathematical functions and operations. The class has the DivRem() function, which is used to compute the quotient and remainder of two specified numbers.

Syntax

This function returns the quotient of two numbers and also calculates the remainder in an output parameter:

DivRem (type param1, type param2, type out remainder);

where:

  • param1: the dividend
  • param2: the divisor
  • remainder: the output parameter which stores the remainder of DivRem()

Variants

DivRem(Int64, Int64, Int64)

  public static long DivRem (long param1, long param2, out long remainder);

DivRem(Int32, Int32, Int32)

  public static int DivRem (int param1, int param2, out int remainder);

Example

using System;
class HelloWorld
{
static void Main()
{
int quotient, rem;
quotient = Math.DivRem(5,3,out rem);
System.Console.WriteLine("Quotient=" + quotient);
System.Console.WriteLine("Remainder="+rem);
}
}

Free Resources