Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Numbers and Math

C# Math.DivRem() method

Reference for C# Math.DivRem(): get the quotient and remainder of an integer division in one call, with tuple and out overloads and sign rules.

Math.DivRem(a, b) divides two integers and returns both the quotient and the remainder at once. It is handy whenever you need both halves of a division: splitting seconds into minutes and seconds, converting a flat index into row and column, or peeling digits off a number.

The tuple-returning overloads (added in .NET 6) are the simplest to use. The older overloads return the quotient and give the remainder through an out parameter. The result follows the same rules as the / and % operators: the quotient truncates toward zero, and the remainder takes the sign of the dividend.

C#
var (minutes, seconds) = Math.DivRem(135, 60);   // (2, 15)

Try it

C# Splitting a duration

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Negative numbers and zero

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static (int Quotient, int Remainder) Math.DivRem(int left, int right)
public static (long Quotient, long Remainder) Math.DivRem(long left, long right)
// also byte, sbyte, short, ushort, uint, ulong, nint, nuint

public static int  Math.DivRem(int a, int b, out int result)
public static long Math.DivRem(long a, long b, out long result)

public static (T Quotient, T Remainder) T.DivRem(T left, T right)  // generic math
Name Description
left / a The dividend.
right / b The divisor.
result (out overloads) receives the remainder.

Returns: a tuple (Quotient, Remainder) or, in the out form, the quotient with the remainder in result.

Errors: DivideByZeroException when the divisor is 0. OverflowException for int.MinValue / -1 (and the same case for long), because the true quotient does not fit.

Usage notes

  • Same semantics as / and %. Math.DivRem(a, b) equals (a / b, a % b). The identity a == quotient * b + remainder always holds.
  • Negative operands. The remainder is negative when the dividend is negative. If you need a result that is never negative (for modular indexing), add the divisor and take the remainder again: ((a % b) + b) % b.
  • Tuple deconstruction. var (q, r) = Math.DivRem(a, b); is the idiomatic form. You can also deconstruct into existing variables.
  • Floating point. There is no Math.DivRem for double. Use Math.Truncate(a / b) and Math.IEEERemainder or the % operator, whose semantics differ for negative or rounding cases.
  • Performance. On many CPUs a single divide instruction yields both values. The JIT can often do the same for separate / and % on the same operands, so use DivRem mainly for clarity.
  • Generic code. int.DivRem and the other type methods come from IBinaryInteger<T>, so generic algorithms can call T.DivRem.