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

Switch Case Exercises

The document contains C++ exercises demonstrating the use of switch-case statements and loops. It includes a program to calculate the number of days in a month, a program to check if a number is odd or even, and a program to print the first 20 odd numbers. Each exercise is accompanied by source code and sample output.

Uploaded by

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

Switch Case Exercises

The document contains C++ exercises demonstrating the use of switch-case statements and loops. It includes a program to calculate the number of days in a month, a program to check if a number is odd or even, and a program to print the first 20 odd numbers. Each exercise is accompanied by source code and sample output.

Uploaded by

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

C++ Exercises Using Switch Case

1. Month Days Calculator


Objective: Create a program that takes the month number (1-12) as input and outputs the
number of days in that month.

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

2. Check if a Number is Odd or Even


Objective: Write a program to determine whether a number is odd or even.
Source Code:

#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

3. Print First 20 Odd Numbers


Objective: Write a C++ program to print the first 20 odd numbers using a loop.

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

You might also like