Write test cases for int divide(int a, int b).......
Software QA/Tests Interview Questions from Microsoft
(Continued from previous question...)
Write test cases for int divide(int a, int b).......
Q:
Microsoft Interview Question for SDE in tests about C
Write test cases for
int divide(int a, int b)
you have to use subtract to get the result!
A:
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
|