1#include <stdio.h>
2int main() {
3 char operator;
4 double first, second;
5 printf("Enter an operator (+, -, *,): ");
6 scanf("%c", &operator);
7 printf("Enter two operands: ");
8 scanf("%lf %lf", &first, &second);
9
10 switch (operator) {
11 case '+':
12 printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
13 break;
14 case '-':
15 printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
16 break;
17 case '*':
18 printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
19 break;
20 case '/':
21 printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
22 break;
23 // operator doesn't match any case constant
24 default:
25 printf("Error! operator is not correct");
26 }
27
28 return 0;
29}
1#include <stdio.h>
2//Simple Calculator
3int main() {
4
5 int fno;
6 int sno;
7 int rst;
8 char opr;
9 printf ( "Enter First Number: ");
10 scanf("%d", &fno);
11 printf ("Enter Second Number: ");
12 scanf("%d", &sno);
13 printf ("Type Operator : ");
14 scanf(" %c", &opr);
15 rst = fno + sno;
16 if (opr == '+')
17
18 printf ("%d \n", fno+sno);
19
20 else (opr == '-');
21 printf ("%d \n", fno-sno);
22
23 if (opr == '*')
24
25 printf("%d \n", fno*sno);
26
27 else (opr == '/');
28 printf ("%d \n", fno/sno);
29}
30
31
1/*C program to design calculator with basic operations using switch.*/
2
3#include <stdio.h>
4
5int main()
6{
7 int num1,num2;
8 float result;
9 char ch; //to store operator choice
10
11 printf("Enter first number: ");
12 scanf("%d",&num1);
13 printf("Enter second number: ");
14 scanf("%d",&num2);
15
16 printf("Choose operation to perform (+,-,*,/,%): ");
17 scanf(" %c",&ch);
18
19 result=0;
20 switch(ch)
21 {
22 case '+':
23 result=num1+num2;
24 break;
25
26 case '-':
27 result=num1-num2;
28 break;
29
30 case '*':
31 result=num1*num2;
32 break;
33
34 case '/':
35 result=(float)num1/(float)num2;
36 break;
37
38 case '%':
39 result=num1%num2;
40 break;
41 default:
42 printf("Invalid operation.\n");
43 }
44
45 printf("Result: %d %c %d = %f\n",num1,ch,num2,result);
46 return 0;
47}