LearnFrenzy
Sign in Take a free test

Control Instructions · Decision Making Programs · Question 4

Day of the week from a number using switch in C Solution, output & explanation

  • Program
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

Print the day name for 1–7 (1 = Monday), mark weekends, and reject invalid numbers.

Solution
main.c
#include <stdio.h>

void day(int d) {
    printf("%d: ", d);
    switch (d) {
    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 (weekend)"); break;
    case 7: puts("Sunday (weekend)");   break;
    default: puts("invalid day");
    }
}

int main(void) {
    int tests[] = {1, 6, 7, 9};
    for (int i = 0; i < 4; i++)
        day(tests[i]);
    return 0;
}

Output Verified by compiling and running

1: Monday
6: Saturday (weekend)
7: Sunday (weekend)
9: invalid day

A classic menu-style switch: one case per value, break after each, and default to catch anything outside 1–7.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links