-
Notifications
You must be signed in to change notification settings - Fork 165
/
CCode.cpp
123 lines (57 loc) · 2.04 KB
/
CCode.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdio.h>
int main()
{
// declare local variables
char opt;
int n1, n2;
float res;
printf (" Choose an operator(+, -, *, /) to perform the operation in C Calculator \n ");
scanf ("%c", &opt); // take an operator
if (opt == '/' )
{
printf (" You have selected: Division");
}
else if (opt == '*')
{
printf (" You have selected: Multiplication");
}
else if (opt == '-')
{
printf (" You have selected: Subtraction");
}
else if (opt == '+')
{
printf (" You have selected: Addition");
}
printf (" \n Enter the first number: ");
scanf(" %d", &n1); // take fist number
printf (" Enter the second number: ");
scanf (" %d", &n2); // take second number
switch(opt)
{
case '+':
res = n1 + n2; // add two numbers
printf (" Addition of %d and %d is: %.2f", n1, n2, res);
break;
case '-':
res = n1 - n2; // subtract two numbers
printf (" Subtraction of %d and %d is: %.2f", n1, n2, res);
break;
case '*':
res = n1 * n2; // multiply two numbers
printf (" Multiplication of %d and %d is: %.2f", n1, n2, res);
break;
case '/':
if (n2 == 0) // if n2 == 0, take another number
{
printf (" \n Divisor cannot be zero. Please enter another value ");
scanf ("%d", &n2);
}
res = n1 / n2; // divide two numbers
printf (" Division of %d and %d is: %.2f", n1, n2, res);
break;
default: /* use default to print default message if any condition is not satisfied */
printf (" Something is wrong!! Please check the options ");
}
return 0;
}