C PROGRAMMING PROGRAM • SWITCH & MENU PROGRAMS
Display a Weekday Using switch
Learn how to display a weekday using switch using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
4
Sample output
Thursday
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
int day;
printf("Enter weekday number (1-7): ");
scanf("%d", &day);
switch (day) {
case 1: puts("Monday"); break;
case 2: puts("Tuesday"); break;
case 3: puts("Wednesday"); break;
case 4: puts("Thursday"); break;
case 5: puts("Friday"); break;
case 6: puts("Saturday"); break;
case 7: puts("Sunday"); break;
default: puts("Invalid weekday number.");
}
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Thursday
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Map each number from one through seven to a weekday case and handle other values with default.
- Display the computed result.
Map each number from one through seven to a weekday case and handle other values with default.
EFFICIENCY
Time and space complexity
Time complexity
O(1)
Auxiliary space
O(1)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
