C Program to Print Day of the Week for Numbers 1 to 7

Introduction:

When learning C programming, it’s common to start with simple input-output tasks. One such task is writing a C program to print day of the week based on user input from 1 to 7. This type of logic-building program helps beginners get comfortable with conditional statements like if-else or switch.


Problem Statement:

Write a C program that takes a number between 1 and 7 as input. Based on the number, the program should display the day of the week:

  • 1 → Sunday
  • 2 → Monday
  • 3 → Tuesday
  • 4 → Wednesday
  • 5 → Thursday
  • 6 → Friday
  • 7 → Saturday
    If the user enters a number outside 1 to 7, the program should show an error message.

C Program Using Switch Statement:

#include <stdio.h>

int main() {
int day;

// Input from user
printf("Enter a number (1 to 7): ");
scanf("%d", &day);

// Print the corresponding day
switch(day) {
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
case 7:
printf("Saturday\n");
break;
default:
printf("Invalid input! Please enter a number between 1 and 7.\n");
}

return 0;
}

Explanation of the Code:

  • The program reads an integer using scanf.
  • It then uses a switch statement to check the value.
  • Each case prints a specific day from Sunday to Saturday.
  • The default case handles invalid inputs.

Why Use Switch Instead of If-Else?

  • Better readability for multiple conditions.
  • Faster execution for discrete values like menu options.
  • Easier to maintain or expand in the future.

Real-World Use Cases:

This logic applies to:

  • Weekly scheduling apps
  • Menu-driven programs
  • Date and time utilities
  • Day-specific reminders


Conclusion:

By using the switch statement, you can efficiently write a C program to print day of the week for numbers 1 to 7. It’s a basic but essential exercise to build your understanding of input handling and control flow in C.

Previous Article

C Program to Prepare Pay Slip with DA, HRA, MA, PF and Net Salary Calculation

Next Article

C Program to Find Maximum and Minimum from 10 Numbers

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨