Find Weekdays using Switch Case in C++


This C++ program takes an input of a day's number and uses a switch statement to output the corresponding day's name. Here's how the program works:

  • The program prompts the user to enter the day's number.
  • The user enters a number between 1 and 7, where 1 is Monday and 7 is Sunday.
  • The program uses a switch statement to check which day was entered.
  • Depending on the entered number, the corresponding day's name is outputted using the cout statement.
  • The break statement is used to end each case statement and prevent the program from executing subsequent case statements.
  • Finally, the program returns 0 to indicate successful execution.

Overall, this program is a good example of how to use a switch statement in C++ to handle multiple cases. However, it could be improved by adding input validation to check that the entered day number is within the correct range of 1 to 7. Additionally, it could be modified to handle upper case letters, or to output a message if an invalid input is entered.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int day;
    cout<<"\nEnter the Day's number :";
    cin>>day;
    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;
    }
    return 0;
}
To download raw file Click Here

Output

Enter the Day's number :3
Wednesday

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples