Write a Simple program in C++ to print Day of the Week using switch statement .
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 | #include <iostream> using namespace std; int main() { int day; // Prompt the user to enter a number cout << "Enter a number (1-7) to get the day of the week: "<<endl; cin >> day; // Determine the day of the week based on the input switch (day) { case 1: cout << "Monday"; break; case 2: cout << "Tuesday"; break; case 3: cout << "Wednesday"; break; case 4: cout << "Thursday"; break; case 5: cout << "Friday"; break; case 6: cout << "Saturday"; break; case 7: cout << "Sunday"; break; default: cout << "Invalid input! Please enter a number between 1 and 7."; break; } return 0; } |
Output
Example Output #1
Enter a number (1-7) to get the day of the week:
1
Monday
Example Output #2
Enter a number (1-7) to get the day of the week:
3
Wednesday
Example Output #3
Enter a number (1-7) to get the day of the week:
5
Friday
Example Output #4
Enter a number (1-7) to get the day of the week:
7
Sunday
Example Output #5
Enter a number (1-7) to get the day of the week:
8
Invalid input! Please enter a number between 1 and 7.
Example Output #6
Enter a number (1-7) to get the day of the week:
0
Invalid input! Please enter a number between 1 and 7.