C# .DivRem()

cheetah3051's avatar
Published Nov 16, 2025
Contribute to Docs

The .DivRem() method calculates the quotient of two numbers and returns the remainder in an output parameter. This method is useful when both the quotient and remainder of a division operation are needed.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
    • Beginner Friendly.
      15 hours

Syntax

public static long DivRem(long dividend, long divisor, out long remainder);

Parameters:

  • dividend: The number to be divided (type int or long).
  • divisor: The number to divide by (type int or long).
  • remainder: An output parameter that receives the remainder (type int or long).

Return value:

The method returns the quotient as an int or long, depending on the parameter types used.

Note: This method throws a DivideByZeroException if the divisor is zero.

Example

The following example demonstrates using the .DivRem() method to calculate both quotient and remainder:

using System;
public class Example
{
public static void Main()
{
int dividend = 30;
int divisor = 7;
int remainder;
int quotient = Math.DivRem(dividend, divisor, out remainder);
Console.WriteLine($"Dividend: {dividend}");
Console.WriteLine($"Divisor: {divisor}");
Console.WriteLine($"Quotient: {quotient}");
Console.WriteLine($"Remainder: {remainder}");
}
}

The example above produces the following output:

Dividend: 30
Divisor: 7
Quotient: 4
Remainder: 2

Codebyte Example

The following runnable example shows how .DivRem() can be used with different numbers:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C# on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
    • Beginner Friendly.
      15 hours