Write a C program to simulate a basic calculator. [+,-,*,/,%]. Use switch statement.
Input Format:
The first line of the input consists of an integer which corresponds to a. The second line of the input consists of a character which corresponds to the operator. The third line of the input consists of an integer which corresponds to b.
Output format: Output consists of a single line [a op b]. Refer to sample output for details.
Sample Input 1:
3
+
5
Sample Output 1:
The sum is 8
Sample Input 2:
7
-
6
Sample Output 2:
The difference is 1
Sample Input 3:
4
*
3
Sample Output 3:
The product is 12
Sample Input 4:
12
/
3
Sample Output 4:
The quotient is 4
Sample Input 5:
4
%
2
Sample Output 5:
The remainder is 0
Sample Input 6:
5
^
2
Sample Output 6:
Invalid Input
Code:
#include<stdio.h> int main() { int a,b; char o[1]; scanf("%d",&a); scanf("%s",o); scanf("%d",&b); switch(o[0]) { case '+': printf("The sum is %d",a+b); break; case '-': printf("The difference is %d",a-b); break; case '*': printf("The product is %d",a*b); break; case '/': printf("The quotient is %d",(int)(a/b)); break; case '%': printf("The remainder is %d",(int)(a%b)); break; default: printf("Invalid Input"); break; } return 0; }
Why is it not working when the character is input through %c?
ReplyDelete