-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcalculator.c
42 lines (41 loc) · 1005 Bytes
/
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
30
31
32
33
34
35
36
37
38
39
40
41
42
// C Program to Make a Simple Calculator
// Using switch case
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
double a, b;
while (1) {
printf("Enter an operator (+, -, *, /), if want to exit press x: ");
scanf(" %c", &ch);
// to exit
if (ch == 'x')
exit(0);
printf("Enter two first and second operand: ");
scanf("%lf %lf",&a,&b);
// Using switch case we will differentiate
// operations based on different operator
switch (ch) {
// For Addition
case '+':
printf("%.1lf + %.1lf = %.1lf\n", a, b, a + b);
break;
// For Subtraction
case '-':
printf("%.1lf - %.1lf = %.1lf\n", a, b, a - b);
break;
// For Multiplication
case '*':
printf("%.1lf * %.1lf = %.1lf\n", a, b, a * b);
break;
// For Division
case '/':
printf("%.1lf / %.1lf = %.1lf\n", a, b, a / b);
break;
// If operator doesn't match any case constant
default:
printf("Error! please write a valid operator\n");
}
}
}