Write a C++ program to print Number of Days in a Month.
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 | #include <iostream> using namespace std; int main() { int month; int days; cout << "Enter the month number (1-12): "; cin >> month; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; cout << "Number of days in month " << month << " is " << days << endl; break; case 4: case 6: case 9: case 11: days = 30; cout << "Number of days in month " << month << " is " << days << endl; break; case 2: days = 28; // Assuming non-leap year cout << "Number of days in month " << month << " is " << days << endl; break; default: cout << "Invalid month number. Please enter a number between 1 and 12." << endl; break; } return 0; } |
Possible Outputs
Output #1
Enter the month number (1-12): 5
Number of days in month 5 is 31
Output #2
Enter the month number (1-12): 6
Number of days in month 6 is 30
Output #3
Enter the month number (1-12): 2
Number of days in month 2 is 28
Output #4
Enter the month number (1-12): 13
Invalid month number. Please enter a number between 1 and 12.