Question
Write a C program to find the day of the week based on an input number using a switch statement
Solution
✔ Verified
This C program takes a number input from the user and displays the corresponding day of the week. The program uses a switch statement, which is an efficient way to handle multiple conditions based on a single variable—in this case, the input number.
The switch statement works by checking the value of day and jumping directly to the correct case, making it an efficient way to handle multiple conditions based on a single value.
#include <stdio.h>
int main() {
int day;
printf("Enter day number (1 to 7): ");
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 day number\n");
}
return 0;
}
Output:
Enter day number (1 to 7): 7
SundayClick here to download practice questions on
Switch statement in CMore Questions on Switch statement in C
Question 1
Write a C program to check if the entered character is a vowel or consonant using a switch statement.
View solution
Question 2
Write a program in C to create a simple calculator using the switch statement.
View solution