C# | Math.DivRem() Method

In C#, Math.DivRem() is a Math class method which divides two numbers and returns the remainder. By using Division operator, we do not get the remainder in a separate variable but using DivRem() method, we get both.
This method can be overloaded by passing different type and number of arguments to it.

  1. Math.DivRem(Int32, Int32, Int32)
  2. Math.DivRem(Int64, Int64, Int64)

Math.DivRem(Int32, Int32, Int32)

This method is used to calculate the quotient of two 32-bit signed integers and returns the remainder in an output parameter.

Syntax:

public static int DivRem(int dividend, int divisor, int result);

Parameter:

dividend: It is an Input Dividend number of type Int32.
divisor: It is an Input Divisor number of type Int32.
result: It is an output Remainder of type Int32.

Return Type: This function returns the quotient of the specified numbers of type Int32.

Exception: If the value of Divisor is zero then this method will give DivideByZeroException.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate working
// of Math.DivRem(Int32, Int32, Int32)
using System;
  
class DivRemGeeks {
  
    // Main method
    static void Main()
    {
  
        // Input +ve  dividend and divisor number
        int dividend_A = 9845324;
        int divisor_A = 7;
  
        // Input negative dividend and divisor number
        int dividend_B = -99999;
        int divisor_B = 2;
  
        int result_1;
        int result_2;
  
        // Using the MATH.DivRem() Method
        int quotient_ONE = Math.DivRem(dividend_A, divisor_A, out result_1);
        int quotient_TWO = Math.DivRem(dividend_B, divisor_B, out result_2);
  
        // Print Result
        Console.WriteLine(quotient_ONE);
        Console.WriteLine(result_1);
        Console.WriteLine(quotient_TWO);
        Console.WriteLine(result_2);
    }
}

chevron_right


Output:

1406474
6
-49999
-1

Math.DivRem(Int64, Int64, Int64)

This method is used to calculates the quotient of two 64-bit signed integers(long) and returns the remainder in an output parameter.

Syntax:

public static long DivRem(int dividend, int divisor, int result);

Parameter:

dividend: It is an Input Dividend number of type Int64.
divisor: It is an Input Divisor number of type Int64.
result: It is an output Remainder of type Int64.

Return Type: This function returns the quotient of the specified numbers of type Int64.

Exception: If the value of Divisor is zero then this method will give DivideByZeroException.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate working
// of Math.DivRem(Int64, Int64, Int64) method
using System;
  
class DivRemGeeks {
  
    // Main method
    static void Main()
    {
  
        // Input +ve dividend and divisor number
        long dividend_A = 9223372036854775779;
        long divisor_A = 2000;
  
        // Input negative dividend and divisor number
        long dividend_B = -99999888844499233;
        long divisor_B = 768933;
  
        long result_1;
        long result_2;
  
        // Using the MATH.DivRem() Method
        long quotient_ONE = Math.DivRem(dividend_A, divisor_A, out result_1);
        long quotient_TWO = Math.DivRem(dividend_B, divisor_B, out result_2);
  
        // Print Result
        Console.WriteLine(quotient_ONE);
        Console.WriteLine(result_1);
  
        Console.WriteLine(quotient_TWO);
        Console.WriteLine(result_2);
    }
}

chevron_right


Output:

4611686018427387
1779
-130050197929
-359476

References: https://docs.microsoft.com/en-us/dotnet/api/system.math.divrem?view=netframework-4.7.2



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.