From df8204fc980486a05a4c7817e2fb55cf6f209f3a Mon Sep 17 00:00:00 2001 From: Yuval Levental Date: Fri, 7 Nov 2025 08:42:58 -0500 Subject: [PATCH 1/2] Add DivRem() method to C# Math Functions --- .../math-functions/terms/divrem/divrem.md | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 content/c-sharp/concepts/math-functions/terms/divrem/divrem.md diff --git a/content/c-sharp/concepts/math-functions/terms/divrem/divrem.md b/content/c-sharp/concepts/math-functions/terms/divrem/divrem.md new file mode 100644 index 00000000000..c1b43f8903f --- /dev/null +++ b/content/c-sharp/concepts/math-functions/terms/divrem/divrem.md @@ -0,0 +1,93 @@ +--- +Title: '.DivRem()' +Description: 'Calculates the quotient of two numbers and returns the remainder in an output parameter.' +Subjects: + - 'Computer Science' + - 'Code Foundations' +Tags: + - 'Methods' + - 'Numbers' + - 'Arithmetic' +CatalogContent: + - 'learn-c-sharp' + - 'paths/computer-science' +--- + +The **`.DivRem()`** method calculates the quotient of two numbers and returns the remainder in an output parameter. This method is useful when both the quotient and remainder of a division operation are needed. + +## Syntax + +```pseudo +Math.DivRem(dividend, divisor, out remainder) +``` + +- `dividend`: The number to be divided (type `int` or `long`). +- `divisor`: The number to divide by (type `int` or `long`). +- `remainder`: An output parameter that receives the remainder (type `int` or `long`). + +The method returns the quotient as an `int` or `long` depending on the parameter types used. + +**Note:** This method throws a `DivideByZeroException` if the `divisor` is zero. + +## Example + +The following example demonstrates using the `.DivRem()` method to calculate both quotient and remainder: + +```cs +using System; + +public class Example +{ + public static void Main() + { + int dividend = 30; + int divisor = 7; + int remainder; + + int quotient = Math.DivRem(dividend, divisor, out remainder); + + Console.WriteLine($"Dividend: {dividend}"); + Console.WriteLine($"Divisor: {divisor}"); + Console.WriteLine($"Quotient: {quotient}"); + Console.WriteLine($"Remainder: {remainder}"); + } +} +``` + +The example above produces the following output: + +```shell +Dividend: 30 +Divisor: 7 +Quotient: 4 +Remainder: 2 +``` + +## Codebyte Example + +The following runnable example shows how `.DivRem()` can be used with different numbers: + +```codebyte/csharp +using System; + +public class Program +{ + public static void Main() + { + // Example with positive numbers + int remainder1; + int quotient1 = Math.DivRem(100, 13, out remainder1); + Console.WriteLine($"100 divided by 13 = {quotient1} remainder {remainder1}"); + + // Example with larger numbers using long + long remainder2; + long quotient2 = Math.DivRem(1000000L, 777L, out remainder2); + Console.WriteLine($"1000000 divided by 777 = {quotient2} remainder {remainder2}"); + + // Example with negative dividend + int remainder3; + int quotient3 = Math.DivRem(-25, 4, out remainder3); + Console.WriteLine($"-25 divided by 4 = {quotient3} remainder {remainder3}"); + } +} +``` From 8ceb5bcb5514b563b4d9f9477962f181a10a5ca3 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 8 Nov 2025 15:41:36 +0530 Subject: [PATCH 2/2] minor content fixes --- .../concepts/math-functions/terms/divrem/divrem.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/content/c-sharp/concepts/math-functions/terms/divrem/divrem.md b/content/c-sharp/concepts/math-functions/terms/divrem/divrem.md index c1b43f8903f..a2f33105b4e 100644 --- a/content/c-sharp/concepts/math-functions/terms/divrem/divrem.md +++ b/content/c-sharp/concepts/math-functions/terms/divrem/divrem.md @@ -2,12 +2,12 @@ Title: '.DivRem()' Description: 'Calculates the quotient of two numbers and returns the remainder in an output parameter.' Subjects: - - 'Computer Science' - 'Code Foundations' + - 'Computer Science' Tags: + - 'Arithmetic' - 'Methods' - 'Numbers' - - 'Arithmetic' CatalogContent: - 'learn-c-sharp' - 'paths/computer-science' @@ -18,13 +18,17 @@ The **`.DivRem()`** method calculates the quotient of two numbers and returns th ## Syntax ```pseudo -Math.DivRem(dividend, divisor, out remainder) +public static long DivRem(long dividend, long divisor, out long remainder); ``` +**Parameters:** + - `dividend`: The number to be divided (type `int` or `long`). - `divisor`: The number to divide by (type `int` or `long`). - `remainder`: An output parameter that receives the remainder (type `int` or `long`). +**Return value:** + The method returns the quotient as an `int` or `long` depending on the parameter types used. **Note:** This method throws a `DivideByZeroException` if the `divisor` is zero.