In this article, we will write a C program to print the day (for example Monday, Tuesday etc.) for an input of date, month and year.
The explanation of the program is at the end of the code.
#include <stdio.h>
#include <stdlib.h>
// Function to calculate the day of the week
int calculateDayOfWeek(int day, int month, int year) {
// Array storing the number of days in each month
int monthDays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// Array storing the names of days
char *days[] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
// Adjust February days for leap years
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
monthDays[2] = 29;
// Validate input
if (month < 1 || month > 12 || day < 1 || day > monthDays[month] || year < 1) {
printf("Invalid input!\n");
exit(EXIT_FAILURE);
}
// Calculate the day of the week using Zeller's Congruence
if (month < 3) {
month += 12;
year--;
}
int h = (day + ((13 * (month + 1)) / 5) + year +
(year / 4) - (year / 100) + (year / 400)) % 7;
return h;
}
int main() {
int day, month, year;
// Input date, month, and year from the user
printf("Enter date (1-31): ");
scanf("%d", &day);
printf("Enter month (1-12): ");
scanf("%d", &month);
printf("Enter year: ");
scanf("%d", &year);
// Calculate and print the day of the week
int dayOfWeek = calculateDayOfWeek(day, month, year);
printf("The day is: %s\n", days[dayOfWeek]);
return 0;
}
Output:
Enter date (1-31): 14
Enter month (1-12): 5
Enter year: 2024
The day is: Tuesday
This means that May 14, 2024, falls on a Tuesday.
Include Header Files:
#include <stdio.h>
#include <stdlib.h>
stdio.h
is included for standard input/output functions such asprintf
andscanf
.stdlib.h
is included for theexit
function used in input validation.
Note: The exit
function in C is used to terminate a program immediately. It is part of the stdlib.h
library and allows you to specify an exit status, which is typically an integer. Based on the exit status, we can determine whether the program terminated successfully or encountered an error.
void exit(int status);
status
: An integer value that indicates the exit status of the program. By convention:0
typically means successful completion.- Non-zero values usually indicate an error.
Function to Calculate Day of the Week:
int calculateDayOfWeek(int day, int month, int year) {
This function takes three integers (day, month, year) and returns the day of the week as an integer (0 for Saturday, 1 for Sunday, …, 6 for Friday).
Array for Days in Each Month:
int monthDays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
This array holds the number of days in each month. Just for the convenience, we stored 0 at the index 0, The number of days in January is stored at monthDays[1]
, February at monthDays[2]
and so on.
Array for Names of Days:
char *days[] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
This array holds the names of the days of the week.
Leap Year Adjustment:
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
monthDays[2] = 29;
This logic updates the monthDays[2] value to 29 from 28 for the leap years.
Input Validation:
if (month < 1 || month > 12 || day < 1 || day > monthDays[month] || year < 1) {
printf("Invalid input!\n");
exit(EXIT_FAILURE);
}
This block validates the input date. If the date is not valid, it prints an error message and exits the program.
Zeller’s Congruence Calculation:
if (month < 3) {
month += 12;
year--;
}
int h = (day + ((13 * (month + 1)) / 5) + year +
(year / 4) - (year / 100) + (year / 400)) % 7;
This block uses Zeller’s Congruence algorithm to calculate the day of the week. The algorithm requires the month and year to be adjusted for January and February (considered as months 13 and 14 of the previous year).
Return Day of the Week:
return h;
Main Function:
int main() {
The main
function where the program execution starts.
User Input:
printf("Enter date (1-31): ");
scanf("%d", &day);
printf("Enter month (1-12): ");
scanf("%d", &month);
printf("Enter year: ");
scanf("%d", &year);
These lines prompt the user to input the date, month, and year.
Calculate and Print Day of the Week:
int dayOfWeek = calculateDayOfWeek(day, month, year);
printf("The day is: %s\n", days[dayOfWeek]);
The calculateDayOfWeek
function is called with the input date, retrieves the day of the week, and prints it.
End of Program:
return 0;
This line indicates that the program has executed successfully and returns 0 to the operating system.
Leave a Reply