Philips Interview Question for Junior programmers
- 0of 0 votes
AnswersCreate a text file called calc.c that contains the following C program:
- stimulation101 March 04, 2017 in England
#include <stdio.h>
float add (float x, float y)
{
return x + y;
}
int main()
{
float a, b, r;
char op;
do {
printf("number op number? ");
scanf(" %f %c %f", &a, &op, &b);
switch (op)
{
case '+' : r = add(a,b);
break;
case 'q' : break;
default : op='?';
}
if (op=='?')
printf("Unknown operator\n");
else if (op=='q')
printf("Bye\n");
else
printf("%f %c %f = %f\n", a, op, b, r);
}
while (op != 'q');
return 0;
}
The program behaves like a calculator asking the user to type in binary expressions (like 2.5 + 3.7) and printing out the results. If the user enters 'q' for the operator (e.g. 0 q 0) then this is taken as a signal to stop the program.
Add a new function to the program that multiplies its two arguments together and returns their product. Then modify the switch statement so that the operator '*' causes this new product function to be called. Test the program by using a mixture of '+' and '*' calculations.
Add operators for subtraction ('-') and division ('/') and test your program again.
We introduce some special operators. Write functions to implement each of these (adding one at a time and testing after each new operator is incorporated into your calculator).
Symbol Description Example
m Minimum 2.3 m 1.7 = 1.7
M Maximum 2.3 M 1.7 = 2.3| Report Duplicate | Flag | PURGE
Philips Junior programmer Programmin in c