Math.DivRem() Method in C#


The Math.DivRem() method in C# is used to divide and calculate the quotient of two numbers and also returns the remainder in an output parameter.

Syntax

public static int DivRem (int dividend, int divisor, out int remainder);
public static long DivRem (long dividend, long divisor, long remainder);

Let us now see an example to implement Math.DivRem() method −

Example

using System;
public class Demo {
   public static void Main(){
      int dividend = 30;
      int divisor = 7;
      int remainder;
      int quotient = Math.DivRem(dividend, divisor, out remainder);
      Console.WriteLine("Quotient = "+quotient);
      Console.WriteLine("Remainder = "+remainder);
   }
}

Output

This will produce the following output −

Quotient = 4
Remainder = 2

Let us see another example to implement Math.DivRem() method −

Example

using System;
public class Demo {
   public static void Main(){
      long dividend = 767676765765765;
      long divisor = 7;
      long remainder;
      long quotient = Math.DivRem(dividend, divisor, out remainder);
      Console.WriteLine("Quotient = "+quotient);
      Console.WriteLine("Remainder = "+remainder);
   }
}

Output

This will produce the following output −

Quotient = 109668109395109
Remainder = 2

Updated on: 04-Nov-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements