0% found this document useful (0 votes)
13 views3 pages

Switch

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Switch

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

6/30/24, 1:27 AM Code

1 /**
2 * C program to create simple calculator using switch case and functions
3 */
4
5 #include <stdio.h>
6
7
8 /**
9 * Function declarations for calculator
10 */
11 float add(float num1, float num2);
12 float sub(float num1, float num2);
13 float mult(float num1, float num2);
14 float div(float num1, float num2);
15
16
17
18 int main()
19 {
20 char op;
21 float num1, num2, result=0.0f;
22
23 /* Print welcome message */
24 printf("WELCOME TO SIMPLE CALCULATOR\n");
25 printf("----------------------------\n");
26 printf("Enter [number 1] [+ - * /] [number 2]\n");
27
28 /* Input two number and operator from user */
29 scanf("%f %c %f", &num1, &op, &num2);
30
31 switch(op)
32 {
33 case '+':
34 result = add(num1, num2);
35 break;
36
37 case '-':
38 result = sub(num1, num2);
39 break;
40
41 case '*':

https://tarikjaber.github.io/Code-to-PDF/ 1/3
6/30/24, 1:27 AM Code
42 result = mult(num1, num2);
43 break;
44
45 case '/':
46 result = div(num1, num2);
47 break;
48
49 default:
50 printf("Invalid operator");
51 }
52
53 /* Print the result */
54 printf("%.2f %c %.2f = %.2f", num1, op, num2, result);
55
56 return 0;
57 }
58
59
60 /**
61 * Function to add two numbers
62 */
63 float add(float num1, float num2)
64 {
65 return num1 + num2;
66 }
67
68 /**
69 * Function to subtract two numbers
70 */
71 float sub(float num1, float num2)
72 {
73 return num1 - num2;
74 }
75
76 /**
77 * Function to multiply two numbers
78 */
79 float mult(float num1, float num2)
80 {
81 return num1 * num2;
82 }
83
84 /**

https://tarikjaber.github.io/Code-to-PDF/ 2/3
6/30/24, 1:27 AM Code
85 * Function to divide two numbers
86 */
87 float div(float num1, float num2)
88 {
89 return num1 / num2;
90 }

https://tarikjaber.github.io/Code-to-PDF/ 3/3

You might also like