Write test cases for int divide(int a, int b) ....
Microsoft Interview Questions and Answers
(Continued from previous question...)
94. Write test cases for int divide(int a, int b) ....
Question:
Write test cases for
int divide(int a, int b)
you have to use subtract to get the result!
maybe an answer:
private static void divideTwoNumbersWithoutDividingOperator(int x, int y)
{
int res = 0;
int rem = 0;
int sign = 1;
//check the sign
sign = (x < 0) ? -1 : 1;
x = x * sign;//change the sign to a positive number
if (x < y)
{
rem = x;
}
while ((x - y) > 0)
{
rem = x - y;
x = x - y;
++res;
}
res = res * sign;//reset the sign
Console.WriteLine("x/y = " + res);
Console.WriteLine("x%y = " + rem);
}
(Continued on next question...)
Other Interview Questions
|