C++ program to print the days of the week by using switch case

C++ program to print the days of the week using switch:

In this C++ program, we will learn how to print the days of the week by using switch statements. The program will take one integer value in the range of 1 to 7 and print the day name represented by that number.

For example, if the entered number is 1, it will print Monday, if it is 2, it will print Tuesday etc. We will use a switch statement to write this program.

Example 1: C++ program by using switch case:

The below program prints the days of the week based on a user-input value:

#include <iostream>
using namespace std;

int main()
{
    int day;

    cout << "Enter the day value: " << endl;
    cin >> day;

    switch (day)
    {
    case 1:
        cout << "Monday" << endl;
        break;
    case 2:
        cout << "Tuesday" << endl;
        break;
    case 3:
        cout << "Wednesday" << endl;
        break;
    case 4:
        cout << "Thursday" << endl;
        break;
    case 5:
        cout << "Friday" << endl;
        break;
    case 6:
        cout << "Saturday" << endl;
        break;
    case 7:
        cout << "Sunday" << endl;
        break;
    default:
        cout << "Please enter a valid input!!" << endl;
    }
}

Here,

  • The integer variable day is used to store the user-input day value.
  • It asks the user to enter the day value, reads it and stores it in the day variable.
  • The switch statement uses the day value to execute the case statements.
  • If the entered value is between 1 to 7, it will print the weekday and exit from the switch statement. Else, it will move to the default block and it will print a message to enter a valid input.

It will print output as below:

Enter the day value: 
2
Tuesday

Enter the day value: 
4
Thursday

Enter the day value: 
33
Please enter a valid input!!

Example 2: C++ program by using a separate function:

Let’s use a separate function to print the day value. This program will use a separate function that will take the integer day value as the parameter and return one string that represents the day in string.

#include <iostream>
using namespace std;

string getDay(int day)
{
    switch (day)
    {
    case 1:
        return "Monday";
    case 2:
        return "Tuesday";
    case 3:
        return "Wednesday";
    case 4:
        return "Thursday";
    case 5:
        return "Friday";
    case 6:
        return "Saturday";
    case 7:
        return "Sunday";
    default:
        return "Please enter a valid input!!";
    }
}
int main()
{
    int day;

    cout << "Enter the day value: " << endl;
    cin >> day;

    string dayString = getDay(day);

    cout << dayString << endl;
}
  • The getDay method is used to convert an integer day value to a string. It takes one integer value as its parameter and returns the day as a string.
  • This method is called from the main method and the returned value is stored in the dayString variable.
  • The last line is printing the value of dayString.

It will print similar output.

C++ example to print days of week

You might also like: