Is it possible to input an operator by character get result ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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; }

1st Jun 2020, 2:16 PM
Abhishek Kumar
Abhishek Kumar - avatar
2 Answers
+ 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
1st Jun 2020, 2:18 PM
Michi
Michi - avatar
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);
1st Jun 2020, 2:46 PM
Ipang