C program to print day of week name using switch case

Previous Program Next Program

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 using switch case. Logic to find day name of week using switch case in C program.

Example

Input

Input week number(1-7): 2

Output

Tuesday

Required knowledge

Basic programming, Switch case

Logic to print day name of week using switch case

Below is the step by step descriptive logic to print day name of week.

  1. Read day number from user, store it in some variable say week.
  2. Switch the value of week and match with cases.
  3. For case 1 print "MONDAY", for case 2 print "TUESDAY" and so on. Print "SUNDAY" for case 7.
  4. For default case print Invalid week number.

Let us code the solution for the given program.

Program to print day name of week using switch case

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

#include <stdio.h>

int main()
{
    int week;
    
    /*
     * Read 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;
}

Before moving on to the next program, practice this program using another approach.

Output
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 ;)

You may also like

Previous Program Next Program
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
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment