Hewlett Packard Interview Question
Software Engineer / Developersvoid ProcessMenu(void)
{
printf("\nCalc with + *: %i", CalcWithOnlyAdd('*', 10, 2));
printf("\nCalc with + -: %i", CalcWithOnlyAdd('-', 4, 8));
printf("\nCalc with + /: %i", CalcWithOnlyAdd('/', 10, 3));
}
int CalcWithOnlyAdd( char Operator, int Num1, int Num2)
{
int Result = 0;
int I, J;
switch (Operator)
{
case '*':
Result = 0;
for (I = 0; I < Num2; I++)
{
Result = Result + Num1;
}
break;
case '-':
Result = Num1;
for (I = 0; I < Num2; I++)
{
Result = Result + (0xffffffff);
}
break;
case '/':
Result = Num1;
J = 0;
while (Result >= Num2)
{
for (I = 0; I < Num2; I++)
{
Result = Result + (0xffffffff);
}
J++;
}
Result = J;
break;
}
return(Result);
}
For * operation
- Niti December 05, 2006We can add the first number, second number of times.
Say i=4, j=5;
//Run a for loop for 5 times(j), add i in each //cycle.
for j->0 to 4
add sum += i;
For - operation.
//Brute force approach
Increment the smaller number and keep a count till it reaches the larger number
For / operation.
follow the method given above for * operation, and keep a count every time you add. That count would be the Quotient.
I hope this answer suffice. Plz correct me if i'm wrong