- Write a C program to print name of days of week using switch case statement.
Required Knowledge
- C printf and scanf functions
- Switch case statement in C
We will take a number between 1 to 7 as input from user, where 1 corresponds to Monday, 2 corresponds to Tuesday and so on. We will use switch case statement to print name of day in words.
C program to print name of day using switch case statement
#include <stdio.h> int main() { int day; /* * Take the Day number as input form user */ printf("Enter Day Number (1 = Monday ..... 7 = Sunday)\n"); scanf("%d", &day); _ switch(day){ case 1 : printf("Monday\n"); break; case 2 : printf("Tuesday\n"); break; case 3 : printf("Wednesday\n"); break; case 4 : printf("Thursday\n"); break; case 5 : printf("Friday\n"); break; case 6 : printf("Saturday\n"); break; case 7 : printf("Sunday\n"); break; default: printf("Invalid Input !!!!\n"); } return 0; }
Output
Enter Day Number (1 = Monday ..... 7 = Sunday) 4 Thursday
Enter Day Number (1 = Monday ..... 7 = Sunday) 7 Sunday
Enter Day Number (1 = Monday ..... 7 = Sunday) 12 Invalid Input !!!!