-
Notifications
You must be signed in to change notification settings - Fork 28
/
calculator.c
94 lines (82 loc) · 3.14 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int action, n1, n2;
float res;
char ch;
do
{
printf (" Select an operation to perform the calculation in C Calculator: ");
printf (" \n 1 Addition \t \t 2 Subtraction \n 3 Multiplication \t 4 Division \n 5 Square \t \t 6 Square Root \n 7 Exit \n \n Please, Make a choice ");
scanf ("%d", &action);
switch (action)
{
case 1:
printf (" You chose: Addition");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
printf (" Enter Second Number: ");
scanf (" %d", &n2);
res = n1 + n2;
printf (" Addition of two numbers is: %.2f", res);
break;
case 2:
printf (" You chose: Subtraction");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
printf (" Enter Second Number: ");
scanf (" %d", &n2);
res = n1 - n2;
printf (" Subtraction of two numbers is: %.2f", res);
break;
case 3:
printf (" You chose: Multiplication");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
printf (" Enter Second Number: ");
scanf (" %d", &n2);
res = n1 * n2;
printf (" Multiplication of two numbers is: %.2f", res);
break;
case 4:
printf (" You chose: Division");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
printf (" Enter Second Number: ");
scanf (" %d", &n2);
if (n2 == 0)
{
printf (" \n Divisor cannot be zero. Please enter another value ");
scanf ("%d", &n2);
}
res = n1 / n2;
printf (" Division of two numbers is: %.2f", res);
break;
case 5:
printf (" You chose: Square");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
res = n1 * n1;
printf (" Square of %d number is: %.2f", n1, res);
break;
case 6:
printf (" You chose: Square Root");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
res = sqrt(n1);
printf (" Square Root of %d numbers is: %.2f", n1, res);
break;
case 7:
printf (" You chose: Exit");
exit(0);
break;
default:
printf(" Something is wrong!! ");
break;
}
printf (" \n \n ********************************************** \n ");
} while (action != 7);
return 0;
}