C#: Calculate the quotient and remainder of two 32-bit signed integers
C# Sharp Math: Exercise-8 with Solution
Write a C# Sharp program to calculate the quotient of two 32-bit signed integers and return the remainder as an output parameter.
Sample Solution:
C# Sharp Code:
using System;
using System.Text;
namespace exercises {
class Program {
public static void Main()
{
// Define several positive and negative dividends.
int[] dividends = { Int32.MaxValue, 23547, 0, -12547,
Int32.MinValue };
// Define one positive and one negative divisor.
int[] divisors = { 4000, -4000 };
foreach (int divisor in divisors)
{
foreach (int dividend in dividends)
{
int remainder;
int quotient = Math.DivRem(dividend, divisor, out remainder);
Console.WriteLine(@"{0:N0} \ {1:N0} = {2:N0}, remainder {3:N0}",
dividend, divisor, quotient, remainder);
}
}
}
}
}
Sample Output:
2,147,483,647 \ 4,000 = 536,870, remainder 3,647 23,547 \ 4,000 = 5, remainder 3,547 0 \ 4,000 = 0, remainder 0 -12,547 \ 4,000 = -3, remainder -547 -2,147,483,648 \ 4,000 = -536,870, remainder -3,648 2,147,483,647 \ -4,000 = -536,870, remainder 3,647 23,547 \ -4,000 = -5, remainder 3,547 0 \ -4,000 = 0, remainder 0 -12,547 \ -4,000 = 3, remainder -547 -2,147,483,648 \ -4,000 = 536,870, remainder -3,648
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program to find the whole number and fractional part from a positive and a negative Decimal number, Double number.
Next: Write a C# Sharp program to calculate the full product of two 32-bit numbers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/csharp-exercises/math/csharp-math-exercise-8.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics