Tech Study

c++ program to print day of week name using switch case

Introduction:

c++ program to print day of week name using switch case

Here’s an example program that does just that:

#include <iostream>

using namespace std;

                       

int main() {

    int dayNumber;




    cout << "Enter a number between 1 and 7: ";

    cin >> dayNumber;




    switch (dayNumber) {

        case 1:

            cout << "Sunday" << endl;

            break;

        case 2:

            cout << "Monday" << endl;

            break;

        case 3:

            cout << "Tuesday" << endl;

            break;

        case 4:

            cout << "Wednesday" << endl;

            break;

        case 5:

            cout << "Thursday" << endl;

            break;

        case 6:

            cout << "Friday" << endl;

            break;

        case 7:

            cout << "Saturday" << endl;

            break;

        default:

            cout << "Invalid input. Please enter a number between 1 and 7." << endl;

            break;

    }




    return 0;

switch c++

c++ program to print day of week name using switch case

Between 1 and 7 number can be entered by the user in program, firstly which represents the day of the week.  In switch c++ case statement will be used to check the value of input and print the corresponding day of the week name

If the user inputs a value outside the range of 1 to 7, the programme will output an error message asking the user to input a valid number

The statement in this program is compare to the user input to each of the seven possible day numbers. The program executes the corresponding case, when the match is found .In this case it prints the name of the day of the week

Ensuring that the program handles the invalid input gracefully, when a default case is included. Between number 1 to 7 range , if the user inputs the number outside of the range the default case will show an error message which will be printed to the console

So, this is a simple and straightforward program that elaborate demonstrates that how to use switch case statements in which C++ performed different action based on users input. This shows that this program can find further extended which included additional logic, which is using the day of the week to .This is how we can find c++ program to print day of week name using switch case.

TaggedWrite C program to print day of week name using switch case

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more