Is it possible to input an operator by character get result ? | SoloLearn: Learn to code for FREE!

0

Is it possible to input an operator by character get result ?

#include <stdio.h> main() { float a,b, result; char op; printf("first number ="); scanf("%f",&a); printf("Enter the operator= %c ",op); scanf(" %c",&op); printf("\nsecond number="); scanf("%d",&b); result= a op b; printf("rsult = %f",result); return 0; }

6/1/2020 2:16:00 PM

Abhishek Kumar

2 Answers

New Answer

+2

I'm not sure, if there is an easier option, but I would use a switch statement and a case for each supported operator

0

Incorrect format specifier used for reading value of variable <b>. Type of variable <b> is a float, so use %f instead of %d, as follows: scanf("%f", &b);