C# Math.DivRem() – Examples

In this tutorial, we will learn about the C# Math.DivRem() method, and learn how to use this method to compute the quotient of division and also get the reminder, with the help of examples.

DivRem(Int32, Int32, Int32)

Math.DivRem(a, b, result) calculates the quotient of two 32-bit signed integers a and b and also returns the remainder in an output parameter result.

Syntax

The syntax of DivRem() method is

</>
Copy
Math.DivRem(Int32 a, Int32 b, out Int32 result)

where

ParameterDescription
aThe dividend of type Int32.
bThe divisor of type Int32.
resultThe remainder of type Int32.

Return Value

The method returns Quotient as return value, and Reminder as output parameter.

Example 1 – DivRem(a, b, result)

In this example, we will take some values for a, b of type Int32 and find the quotient and reminder using Math.DivRem() method.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        Int32 a, b, result, quot;

        a = 10;
        b = 3;
        quot = Math.DivRem(a, b, out result);
        Console.WriteLine($"Division of ({a}, {b}) : Reminder = {result}, Quotient = {quot}");

        a = 10;
        b = 2;
        quot = Math.DivRem(a, b, out result);
        Console.WriteLine($"Division of ({a}, {b}) : Reminder = {result}, Quotient = {quot}");

        a = 10;
        b = 4;
        quot = Math.DivRem(a, b, out result);
        Console.WriteLine($"Division of ({a}, {b}) : Reminder = {result}, Quotient = {quot}");


    }
}

Output

Division of (10, 3) : Reminder = 1, Quotient = 3
Division of (10, 2) : Reminder = 0, Quotient = 5
Division of (10, 4) : Reminder = 2, Quotient = 2

DivRem(Int64, Int64, Int64)

Math.DivRem(a, b, result) calculates the quotient of two 64-bit signed integers a and b; and also returns the remainder in an output parameter result.

Syntax

The syntax of DivRem() method is

</>
Copy
Math.DivRem(Int64 a, Int64 b, out Int64 result)

where

ParameterDescription
aThe dividend of type Int64.
bThe divisor of type Int64.
resultThe remainder of type Int64.

Return Value

The method returns Quotient as return value, and Reminder as output parameter.

Example 2 – DivRem(a, b, result)

In this example, we will take some values for a, b of type Int64 and find the quotient and reminder using Math.DivRem() method.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        Int64 a, b, result, quot;

        a = 10;
        b = 3;
        quot = Math.DivRem(a, b, out result);
        Console.WriteLine($"Division of ({a}, {b}) : Reminder = {result}, Quotient = {quot}");

        a = 10;
        b = 2;
        quot = Math.DivRem(a, b, out result);
        Console.WriteLine($"Division of ({a}, {b}) : Reminder = {result}, Quotient = {quot}");

        a = 10;
        b = 4;
        quot = Math.DivRem(a, b, out result);
        Console.WriteLine($"Division of ({a}, {b}) : Reminder = {result}, Quotient = {quot}");


    }
}

Output

Division of (10, 3) : Reminder = 1, Quotient = 3
Division of (10, 2) : Reminder = 0, Quotient = 5
Division of (10, 4) : Reminder = 2, Quotient = 2

Conclusion

In this C# Tutorial, we have learnt the syntax of C# Math.DivRem() method, and also learnt how to use this method with the help of C# example programs.