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.
- Read day number from user, store it in some variable say week.
- Switch the value of week and match with cases.
- For case 1 print "MONDAY", for case 2 print "TUESDAY" and so on. Print "SUNDAY" for case 7.
- 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
- Switch case programming exercise index
- C program to check whether an alphabet is vowel or consonant using switch case
- C program to print number of days in a month using switch case
- C program to find maximum between two numbers using switch case
- C program to find even or odd using switch case.
- C program to check whether a triangle is Equilateral, Isosceles or Scalene triangle.
- C program to create Simple calculator using switch case.