C# .DivRem()
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.
Syntax
public static long DivRem(long dividend, long divisor, out long remainder);
Parameters:
dividend: The number to be divided (typeintorlong).divisor: The number to divide by (typeintorlong).remainder: An output parameter that receives the remainder (typeintorlong).
Return value:
The method returns the quotient as an int or long, depending on the parameter types used.
Note: This method throws a
DivideByZeroExceptionif thedivisoris 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: 30Divisor: 7Quotient: 4Remainder: 2
Codebyte Example
The following runnable example shows how .DivRem() can be used with different numbers:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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