-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIf-Else and Switch case
333 lines (285 loc) · 7.1 KB
/
If-Else and Switch case
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <iostream>
using namespace std;
// wap to find max age among two students
void maxAge()
{
int age1, age2;
cout << "Enter age of two students : " << endl;
cin >> age1 >> age2;
if (age1 > age2)
{
cout << "Student 1st is older than student 2 " << endl;
}
else if (age1 == age2)
{
cout << "Both students are of same age " << endl;
}
else
{
cout << "Student 2nd is older than student 1st " << endl;
}
}
// wap to find largest number among three numbers
void largestAmong3Numbers()
{
int a, b, c;
cout << "Enter three numbers : " << endl;
cin >> a >> b >> c;
if (a > b & a > c)
{
cout << a << " is the largest " << endl;
}
else if (b > a & b > c)
{
cout << b << " is the largest " << endl;
}
else if (c > a & c > b)
{
cout << c << " is the largest " << endl;
}
}
// wap to find whether number is negative or positive
void posNeg()
{
int a;
cout << "Enter number to find it is negative or positive : " << endl;
cin >> a;
if (a < 0)
{
cout << a << " is a negative number " << endl;
}
else if (a > 0)
{
cout << a << " is a positive number " << endl;
}
else
{
cout << "number entered is = 0" << endl;
}
}
// wap to check number is even or odd
void evenOdd()
{
int n;
cout << "Enter number to find it is Even or Odd " << endl;
cin >> n;
if (n % 2 == 0)
{
cout << n << " is an Even number " << endl;
}
else
{
cout << n << " is a Odd number " << endl;
}
}
// wap to check year is leap year or not
void leapYear()
{
int year;
cout << "Enter year : " << endl;
if (year % 400 == 0)
{
cout << year << " is a leap year " << endl;
}
else if (year % 100 == 0)
{
cout << year << " is not a leap year " << endl;
}
else if (year % 4 == 0)
{
cout << year << " is a leap year " << endl;
}
else
{
cout << year << " is not a leap year " << endl;
}
}
// wap to check character is vowel or consonant
void vowelOrConsonant()
{
char ch;
cout << "Enter a alphabet : " << endl;
cin >> ch;
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
cout << ch << " is a vowel " << endl;
}
else
{
cout << ch << " is a consonant " << endl;
}
}
// largest among two numbers
void largestAmong2Num()
{
int a, b;
cout << "Enter two numbers : " << endl;
cin >> a >> b;
if (a > b)
{
cout << a << " is a larger number " << endl;
}
else if (b > a)
{
cout << b << " is a larger number " << endl;
}
else
{
cout << a << " and " << b << " are eqaul " << endl;
}
}
// ???? Swich case programs ????//
// wap to make simple calculations for all arithmatic operations
void arithmaticCalculations()
{
int op;
cout << "Enter 1 to perform Addition : " << endl;
cout << "Enter 2 to perform Substraction : " << endl;
cout << "Enter 3 to perform Multiplicaton : " << endl;
cout << "Enter 4 to perform Division : " << endl;
cin >> op;
float a, b, result;
cout << "Enter a number : " << endl;
cin >> a;
cout << "Enter another number : " << endl;
cin >> b;
switch (op)
{
case 1:
result = a + b;
cout << "Addition of " << a << " and " << b << " is " << result << endl;
break;
case 2:
result = a - b;
cout << "Subtraction of " << a << " and " << b << " is " << result << endl;
break;
case 3:
result = a * b;
cout << "Multiplication of " << a << " and " << b << " is " << result << endl;
break;
case 4:
result = a / b;
cout << "Division of " << a << " and " << b << " is " << result << endl;
break;
}
}
// wap to check vowel or consonant
void vowelOrConsonantSwitch()
{
char ch;
cout << "Enter a character : " << endl;
cin >> ch;
switch (ch)
{
case 'a':
cout << "Entered alphabet is a vowel : " << endl;
break;
case 'e':
cout << "Entered alphabet is a vowel : " << endl;
break;
case 'i':
cout << "Entered alphabet is a vowel : " << endl;
break;
case 'o':
cout << "Entered alphabet is a vowel : " << endl;
break;
case 'u':
cout << "Entered alphabet is a vowel : " << endl;
break;
default:
cout << "Entered alphabet is a consonant : " << endl;
}
}
void dayOfWeek()
{
int day;
cout << "Enter number : " << endl;
cin >> day;
switch (day)
{
case 1:
cout << "Monday " << endl;
break;
case 2:
cout << "Tuesday " << endl;
break;
case 3:
cout << "Wednesday " << endl;
break;
case 4:
cout << "Thursday " << endl;
break;
case 5:
cout << "Friday " << endl;
break;
case 6:
cout << "Saturday " << endl;
break;
case 7:
cout << "Sunday " << endl;
break;
}
}
int main()
{
cout << " -+-+-+-+-> All programs : Part - 2 <-+-+-+-+- " << endl;
cout << endl;
// programs using if-else
cout << " -+-+-+-+-> Programs using if-Else <-+-+-+-+- " << endl;
cout << endl;
cout << "Enter number to run respective program : " << endl;
cout << endl;
cout << "Enter 1 to find maximum age of two students : " << endl;
cout << "Enter 2 to find the largest number among three numbers : " << endl;
cout << "Enter 3 to find whether a number is negative or positive : " << endl;
cout << "Enter 4 to check if the number is even or odd : " << endl;
cout << "Enter 5 to check whether a year is leap year or no t : " << endl;
cout << "Enter 6 to check whether entered character is vowel or consonant : " << endl;
cout << "Enter 7 to find the largest number among two numbers : " << endl;
// programs using switch case
cout << endl;
cout << " -+-+-+-+-> Programs using Switch case <-+-+-+-+- " << endl;
cout << endl;
cout << "Enter 8 to make simple calculation for all arithmatic operations ( + , - , * , / ) : " << endl;
cout << "Enter 9 to check whether the entered character is vowel or consonant : " << endl;
cout << "Enter 10 to display day of the week as users choice : " << endl;
int count;
cout << endl;
cout << "Enter program number that you are willing to run : " << endl;
cin >> count;
cout << endl;
switch (count)
{
case 1:
maxAge();
break;
case 2:
largestAmong3Numbers();
break;
case 3:
posNeg();
break;
case 4:
evenOdd();
break;
case 5:
leapYear();
break;
case 6:
vowelOrConsonant();
break;
case 7:
largestAmong2Num();
break;
case 8:
arithmaticCalculations();
break;
case 9:
vowelOrConsonantSwitch();
break;
case 10:
dayOfWeek();
break;
}
return 0;
}