-
Notifications
You must be signed in to change notification settings - Fork 0
/
22_Calculator.c
29 lines (26 loc) · 1.02 KB
/
22_Calculator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>
int main(){
char operator;
double a, b;
printf("Enter an operator (+, -, *, /):"); //It will print the operater to be entered by user.
scanf("%c", &operator); //It will scan the operater entered by user.
printf("Enter two operands:");
scanf("%lf %lf",&a, &b); //It will scan the numbers entered by user.
switch(operator) //Switch case is used to perfom the calculation case that is scanned (in line 6) based on user input.
{
case '+':
printf("%.2lf + %.2lf = %.2lf",a, b, a+b); //Addition Function
break;
case '-':
printf("%.2lf - %.2lf = %.2lf",a, b, a-b); //Subtraction Function
break;
case '*':
printf("%.2lf * %.2lf = %.2lf",a, b, a*b); //Multiplication Function
break;
case '/':
printf("%.2lf / %.2lf = %.2lf",a, b, a/b); //Division Function
break;
printf("Error encountered by the input values"); //Default
}
return 0;
}