C# Math DivRem() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In C#, Math.DivRem(a, b) divides two whole numbers and gives you both answers at once: the quotient (how many times b fits into a) and the remainder (what is left over). Math.DivRem(17, 5) gives a quotient of 3 and a remainder of 2, because 5 x 3 + 2 = 17.

Think of sharing 17 sweets among 5 children. Each child gets 3, and 2 sweets are left over. Normally you would write 17 / 5 and 17 % 5 separately. DivRem does both in one go, which keeps the code short and clear. There are two ways to call it: the older form that returns the quotient and fills an out variable with the remainder, and the newer form (.NET 6 and later) that returns a tuple.

This is a static member of the static class System.Math. You call it with the class name, such as Math.DivRem(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. The out version Math.DivRem(a, b, out remainder) has been in .NET for a long time. The tuple version that returns (Quotient, Remainder) needs .NET 6 or later.

Syntax

Pass the number to divide, then the divisor. Take the remainder through an out variable, or take both answers as a tuple.

C#
// older form: remainder comes back through out
int quotient = Math.DivRem(17, 5, out int remainder);   // 3 and 2

// .NET 6 and later: a tuple (Quotient, Remainder)
var (q, r) = Math.DivRem(17, 5);                        // q = 3, r = 2
CallWhat it does
Math.DivRem(17, 5, out int r)Returns 3 and sets r to 2.
var (q, r) = Math.DivRem(17, 5).NET 6 or later. q is 3 and r is 2.
Math.DivRem(-7, 2)Returns (-3, -1). The quotient is cut toward zero.
Math.DivRem(10, 0)Throws DivideByZeroException.

The quotient is cut toward zero and the remainder takes the sign of the first number, exactly like the / and % operators. The relation a == b * quotient + remainder always holds. Dividing by zero throws DivideByZeroException. Overloads exist for int and long, and newer .NET versions add more number types.

Library declarations
C#
public static int DivRem(int a, int b, out int result);
public static long DivRem(long a, long b, out long result);
public static (int Quotient, int Remainder) DivRem(int left, int right);       // .NET 6+
public static (long Quotient, long Remainder) DivRem(long left, long right);   // .NET 6+

Parameters

ParameterDescription
a / leftRequired. The number to divide (the dividend).
b / rightRequired. The number to divide by (the divisor). It must not be 0.
resultOnly in the older form. An out variable that receives the remainder.

Return Value

Return ValueDescription
int / longThe older form returns the quotient, and the remainder comes out through result.
(Quotient, Remainder)The newer form returns a tuple with both parts, for example (3, 2).

C# Examples

Example 1 — Share 17 Sweets Among 5 Children

Call the out version, then the tuple version, and check that the numbers add back up.

C#
using System;

class Program {
    static void Main() {
        int quotient = Math.DivRem(17, 5, out int remainder);
        Console.WriteLine(quotient);
        Console.WriteLine(remainder);
        Console.WriteLine($"17 = 5 x {quotient} + {remainder}");

        var (q, r) = Math.DivRem(17, 5);
        Console.WriteLine(q == quotient && r == remainder);
    }
}

How It Works

  1. The out version. The return value is the quotient, 3. The remainder arrives in the new variable remainder, declared right inside the call with out int.
  2. The remainder. 5 x 3 is 15, and 17 - 15 leaves 2.
  3. Check it. The interpolated string shows that 5 x 3 + 2 brings back 17. This always works for DivRem.
  4. The tuple version. On .NET 6 or later, var (q, r) = ... unpacks the two parts. They match the first call, so the last line prints True.

Example 2 — Turn Seconds into Hours, Minutes, and Seconds

Use two calls to DivRem in a row to split 3725 seconds into a clock time (needs .NET 6 or later).

C#
using System;

class Program {
    static void Main() {
        int total = 3725;

        var (hours, rest) = Math.DivRem(total, 3600);
        var (minutes, seconds) = Math.DivRem(rest, 60);

        Console.WriteLine($"{hours:D2}:{minutes:D2}:{seconds:D2}");
        Console.WriteLine(hours * 3600 + minutes * 60 + seconds);
    }
}

How It Works

  1. Hours first. DivRem(3725, 3600) gives 1 whole hour and 125 seconds left over. The left-over goes into rest.
  2. Then minutes. DivRem(125, 60) gives 2 whole minutes and 5 seconds left over. Each call peels off one unit.
  3. Padding. {hours:D2} means "show at least two digits", so 1 becomes 01. The result reads 01:02:05.
  4. Rebuild check. 1 x 3600 + 2 x 60 + 5 is 3725, the number we started with. Nothing was lost.

Example 3 — Negative Numbers and Dividing by Zero

See how signs behave, how it matches the / and % operators, and what happens when the divisor is zero.

C#
using System;

class Program {
    static void Main() {
        var first = Math.DivRem(-7, 2);
        Console.WriteLine(first.Quotient);
        Console.WriteLine(first.Remainder);

        var second = Math.DivRem(7, -2);
        Console.WriteLine(second.Quotient);
        Console.WriteLine(second.Remainder);

        Console.WriteLine(-7 / 2 == first.Quotient && -7 % 2 == first.Remainder);

        int zero = 0;
        try {
            Math.DivRem(10, zero, out int ignored);
        } catch (DivideByZeroException) {
            Console.WriteLine("cannot divide by zero");
        }
    }
}

How It Works

  1. -7 divided by 2. The quotient is cut toward zero, so it is -3, not -4. The remainder is -1 because -3 x 2 + (-1) = -7.
  2. 7 divided by -2. The quotient is again -3. The remainder takes the sign of the first number, 7, so it is 1.
  3. Same as / and %. -7 / 2 and -7 % 2 give exactly the same two answers, so the comparison is True. DivRem just returns them together.
  4. Zero divisor. A whole-number division by 0 throws DivideByZeroException. The catch block prints a message. The variable zero is used so the compiler does not complain about a constant 0.

Frequently Asked Questions

It divides two whole numbers and returns both the quotient and the remainder. Math.DivRem(17, 5, out int r) returns 3 and sets r to 2.
The form that returns a tuple, var (q, r) = Math.DivRem(a, b), needs .NET 6 or later. The form with an out parameter has been available for much longer.
Yes, it gives the same two answers, including the signs for negative numbers. It simply returns them together, which can be slightly faster and shorter to write.
It throws DivideByZeroException. Check the divisor first, or catch the exception. Math.DivRem(int.MinValue, -1) also throws, an OverflowException.
No. Math is a static class, so you call Math.DivRem(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.

Next: Exp()

Return e raised to a power, where e is about 2.71828.

Exp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful