Mapping Numbers to Weekdays

Q.Write a C++ program to accept a number and display the corresponding number of week day
#include <iostream>
using namespace std;
int main() {
int day;
cout << "Enter a number (1-7) to display the corresponding weekday: ";
cin >> day;
switch(day) {
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 Number! Please enter a number between 1 & 7." << endl;
break;
}
return 0;
}
Output
Enter a number (1-7) to display the corresponding weekday : 7
The day is Saturday.
Determine the Weekday:
switch(day) {
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 Number! Please enter a number between 1 & 7." << endl;
break;
}
We use a
switchstatement to match the input number to the corresponding day of the week.Each
caserepresents a different day. For example, if the input is1, the program outputs "The day is Sunday."The
defaultcase handles invalid input, ensuring the user knows they must enter a number between 1 and 7.
Explanation of the Output
If the user inputs a number between 1 and 7, the program will match the number to the corresponding day of the week using a
switchstatement and print the day.If the user inputs a number outside the range of 1 to 7, the
defaultcase in theswitchstatement will be executed, displaying an error message.
This output mechanism ensures that the user is guided to enter a valid number for the days of the week and is informed when an invalid input is provided.


