C# Tutorial C# Advanced C# References

C# Math - DivRem() Method



The C# DivRem() method is used to calculate the quotient of two numbers and also returns the remainder in an output parameter. The method can be overloaded and it can take int and long arguments.

Syntax

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

Parameters

a Specify the dividend.
b Specify the divisor.
result Specify the name of the variable.

Return Value

Returns the quotient of specified numbers.

Example:

In the example below, DivRem() method is used to calculate the quotient of two numbers. The remainder is stored in the variable called remainder.

using System;

class MyProgram {
  static void Main(string[] args) {
    int quot = Math.DivRem(100, 30, out int remainder);
    
    Console.WriteLine("When 100 is divided by 30 it gives:");
    Console.WriteLine("quotient = " + quot); 
    Console.WriteLine("remainder = " + remainder); 
  }
}

The output of the above code will be:

When 100 is divided by 30 it gives:
quotient = 3
remainder = 10

❮ C# Math Methods