C# Math.DivRem Method

The Math.DivRem method in C# performs integer division and returns both the quotient and remainder in a single operation. This is more efficient than performing separate division and modulo operations when you need both results.

Syntax

Following is the syntax for the Math.DivRem method −

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

Parameters

  • a − The dividend (number to be divided)
  • b − The divisor (number to divide by)
  • result − An out parameter that receives the remainder

Return Value

The method returns the quotient of the division operation, while the remainder is stored in the out parameter.

Math.DivRem Operation Dividend 98 ÷ Divisor 9 Quotient: 10 Remainder: 8

Using Math.DivRem with Integer Values

Example

using System;

class Demo {
   static void Main() {
      // remainder
      long rem;
      // dividend
      long dividend = 98;
      // divisor
      long divisor = 9;
      long quotient = Math.DivRem(dividend, divisor, out rem);
      Console.WriteLine("{0} \ {1} = {2} and remainder = {3}", dividend, divisor, quotient, rem);
   }
}

The output of the above code is −

98 \ 9 = 10 and remainder = 8

Using Math.DivRem with Different Data Types

Example

using System;

class Program {
   static void Main() {
      // Using int version
      int intRemainder;
      int intQuotient = Math.DivRem(47, 5, out intRemainder);
      Console.WriteLine("Int division: 47 / 5 = {0} remainder {1}", intQuotient, intRemainder);
      
      // Using long version
      long longRemainder;
      long longQuotient = Math.DivRem(1234567890L, 123L, out longRemainder);
      Console.WriteLine("Long division: {0} / {1} = {2} remainder {3}", 1234567890L, 123L, longQuotient, longRemainder);
   }
}

The output of the above code is −

Int division: 47 / 5 = 9 remainder 2
Long division: 1234567890 / 123 = 10037170 remainder 100

Practical Use Case

Example

using System;

class TimeConverter {
   static void Main() {
      int totalSeconds = 7890;
      int remainder;
      
      // Convert seconds to hours and remaining seconds
      int hours = Math.DivRem(totalSeconds, 3600, out remainder);
      
      // Convert remaining seconds to minutes and seconds
      int minutes = Math.DivRem(remainder, 60, out int seconds);
      
      Console.WriteLine("{0} seconds = {1} hours, {2} minutes, {3} seconds", 
                       totalSeconds, hours, minutes, seconds);
   }
}

The output of the above code is −

7890 seconds = 2 hours, 11 minutes, 30 seconds

Conclusion

The Math.DivRem method provides an efficient way to perform integer division when you need both the quotient and remainder. It eliminates the need for separate division and modulo operations, making your code more efficient and readable, especially in scenarios like time conversions or pagination calculations.

Updated on: 2026-03-17T07:04:35+05:30

272 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements