C program to enter week number and print day of week name using switch case

Write a C program to enter week number(1-7) and print day of week name using switch case. C program to find week day name.
View this program using if else - C program to print day of week using if else.

Example:
Input week number(1-7): 2
Output: Tuesday

Required knowledge:

Basic programming, Switch case.

Program:

/**
 * C program to print day of week using switch case
 */

#include <stdio.h>

int main()
{
    int week;
    
    /*
     * Reads week number from user
     */
    printf("Enter week number(1-7): ");
    scanf("%d", &week);
    
    switch(week)
    {
        case 1: printf("MONDAY");
            break;
        case 2: printf("TUESDAY");
            break;
        case 3: printf("WEDNESDAY");
            break;
        case 4: printf("THURSDAY");
            break;
        case 5: printf("FRIDAY");
            break;
        case 6: printf("SATURDAY");
            break;
        case 7: printf("SUNDAY");
            break;
        default: printf("Invalid input! Please enter week number between 1-7");
    }
    return 0;
}
Output
X
_
Enter week number(1-7): 1

MONDAY

Note: Here we are assuming Monday as the first day of week. Hence if week number is 1 output will be Monday and so on.

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment