This program allows to simulate the operation of an integer division between 2 positive integers a and b entered, we divide the largest over the smallest without using the operator “/” and then display the quotient and the remainder.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
class Program { static int division(int num1, int num2) { if (num1 == 0) return 0; if (num2 == 0) return int.MaxValue; bool negResult = false; if (num1 < 0) { num1 = -num1; if (num2 < 0) num2 = -num2; else negResult = true; } else if (num2 < 0) { num2 = -num2; negResult = true; } int quotient = 0; while (num1 >= num2) { num1 = num1 - num2; quotient++; } if (negResult) quotient = -quotient; return quotient; } public static void Main() { int num1, num2; Console.Write("Enter Number 1:"); num1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter Number 2:"); num2 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Quotient:{0}",division(num1, num2)); Console.ReadLine(); } } |
Output:
1 2 3 4 5 |
Enter Number 1:25 Enter Number 2:7 Quotient:3 |