Switch Case Exercises
Switch Case Exercises
Source Code:
#include <iostream>
using namespace std;
int main() {
int month;
cout << "Enter month number (1-12): ";
cin >> month;
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
cout << "31 days";
break;
case 4: case 6: case 9: case 11:
cout << "30 days";
break;
case 2:
cout << "28 or 29 days (leap year dependent)";
break;
default:
cout << "Invalid month number";
}
return 0;
}
Output:
Enter month number (1-12): 4
30 days
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
switch (num % 2) {
case 0:
cout << "Even number";
break;
case 1:
cout << "Odd number";
break;
}
return 0;
}
Output:
Enter a number: 7
Odd number
Source Code:
#include <iostream>
using namespace std;
int main() {
cout << "First 20 odd numbers: ";
for (int i = 1, count = 0; count < 20; i += 2, count++) {
cout << i << " ";
}
return 0;
}
Output:
First 20 odd numbers: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39