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);
}
}
New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources