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 weekNumber; // Input the week number cout << "Enter the week number (1-7): "; cin >> weekNumber; // Determine the day of the week using a switch statement switch (weekNumber) { case 1: cout << "The day is Sunday." << endl; break; case 2: cout << "The day is Monday." << endl; break; case 3: cout << "The day is Tuesday." << endl; break; case 4: cout << "The day is Wednesday." << endl; break; case 5: cout << "The day is Thursday." << endl; break; case 6: cout << "The day is Friday." << endl; break; case 7: cout << "The day is Saturday." << endl; break; default: cout << "Invalid week number! Please enter a number between 1 and 7." << endl; break; } return 0; } |
Explanation:
- Input:
- The program prompts the user to enter a week number between 1 and 7.
- Switch Statement:
- The
switch
statement checks the value ofweekNumber
. - Each
case
corresponds to a specific day of the week:case 1
: Outputs “Sunday.”case 2
: Outputs “Monday.”case 3
: Outputs “Tuesday.”case 4
: Outputs “Wednesday.”case 5
: Outputs “Thursday.”case 6
: Outputs “Friday.”case 7
: Outputs “Saturday.”
- The
default
case handles any input outside the range of 1-7, displaying an error message.
- The
- Output:
- Depending on the input, the program prints the corresponding day of the week.
Example Outputs:
Example 1: Valid Input
Input:
- Week Number:
3
Output:
12Enter the week number (1-7): 3The day is Tuesday.Example 2: Valid Input
Input:
- Week Number:
6
Output:
12Enter the week number (1-7): 6The day is Friday.Example 3: Invalid Input
Input:
- Week Number:
8
Output:
12Enter the week number (1-7): 8Invalid week number! Please enter a number between 1 and 7.
- Week Number:
- Depending on the input, the program prints the corresponding day of the week.