Find the Day of the Given Date Using C

Find the Day of the Given Date Using C

5 mins read116 Views Comment
Updated on Oct 5, 2023 19:14 IST

Unlock the secrets of C programming to reveal the day of the week for any date! Dive into Zeller’s Congruence, leap years, and more. Master the art of coding for date manipulation with our comprehensive guide.

2023_10_Find-day-of-week.jpg

Have you ever needed to know what day of the week a particular date falls on? Whether you’re planning an event or just curious, it can be helpful information. The good news is that you can easily find out using the C programming language!

This article will explain how you can write programs in C that calculate the day of the week for any given date.

Understanding the Logic of Leap Year Calculation

It takes one year for the Earth to orbit the Sun. That is nearly 365.25 days long. So, if a year is 365 days, one day will be left over after every four years. 

This is why we have a leap year every four years, which has 366 days. All four-digit years divisible by four, such as 0004 and 0008, are leap years, starting with 0001. 

However, the approximation of 365.25 days is only partially accurate. It leads to anomalies every 100 or 400 years. For example, 0100, 0200, and 0300 are not leap years, but the year 0400 is a leap year.

A year has 12 months, with the following number of days in each month. 

  • February has 28 days (in non-leap years) and 29 days in a leap year
  • March has 31 days
  • April has 30 days
  • May has 31 days 
  • June has 30 days 
  • July has 31 days 
  • August has 31 days 
  • September has 30 days 
  • October has 31 days
  • November has 30 days 
  • December has 31 days 

Therefore, the total number of days in a non-leap year is 365. In a leap year, it is 366.

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

– / –
40 hours
4 K
2 months
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
1 month
– / –
50 hours
– / –
4 months
5.5 K
140 hours

C Programs to Find the Day of the Given Date

Using Switch Case

In this approach, we will use Zeller’s Congruence method to calculate the days of the week. 

Zeller’s Congruence 

Zeller’s congruence is a mathematical formula that determines the day of the week for any given date in the Gregorian calendar. The formula was developed by German mathematician Christian Zeller in 1887.

Zeller’s congruence takes the following form:

h = (q + ((13 * (m + 1)) / 5) + K + (K / 4) + (J / 4) – (2 * J)) % 7

Where:

  • h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, …, 6 = Friday).
  • q is the day of the month.
  • m is the month (3 = March, 4 = April, 5 = May, …, 14 = February).
  • K is the year of the century (year % 100). For example, for the year 2022, K would be 22.
  • J is the zero-based century. For example, for the year 2022, J would be 20 (because it’s the 21st century, but we start counting from 0).

For example, 

Date: 15/02/2022

Adjusting for January and February:

  • Since February is less than March, we treat it as the 14th month of the previous year.
  • m becomes 14.
  • The year becomes 2021.

Plugging in the values:

  • q = 15 (day of the month)
  • m = 14 (adjusted month)
  • K = 2021 % 100 = 21
  • J = 2021 / 100 = 20

Applying the formula:

  • h = (15 + floor(13(14+1)/5) + 21 + floor(21/4) + floor(20/4) – 2(20)) mod 7
  • h = (15 + 91 + 21 + 5 + 5 – 40) mod 7
  • h = 97 mod 7
  • h = 6

Interpreting the result:

The value of 

  • h is 6, which corresponds to Friday.
  • So, 15th February 2022 was a Friday.

This is how the program uses Zeller’s Congruence to determine the day of the week for a given date.

Zeller’s congruence assumes the Gregorian calendar, so it is only accurate for dates after October 4, 1582. 

To better understand the program given below, it is recommended that you have prior knowledge of the Switch Case in C Programming topic.

Here’s the C program that uses switch case to find the day of the given date.


 
#include <stdio.h>
int main() {
int day, month, year;
int day_of_week;
printf("Enter the date in format DD/MM/YYYY: ");
scanf("%d/%d/%d", &day, &month, &year);
if (month < 3) {
month += 12;
year--;
}
day_of_week = (day + (13 * (month + 1) / 5) + (year % 100) +
((year % 100) / 4) + ((year / 100) / 4) + 5 * (year / 100)) % 7;
switch (day_of_week) {
case 0:
printf("The day is Saturday.\n");
break;
case 1:
printf("The day is Sunday.\n");
break;
case 2:
printf("The day is Monday.\n");
break;
case 3:
printf("The day is Tuesday.\n");
break;
case 4:
printf("The day is Wednesday.\n");
break;
case 5:
printf("The day is Thursday.\n");
break;
case 6:
printf("The day is Friday.\n");
break;
}
return 0;
}
Copy code

Output

2023_10_image.jpg

Explanation:

  • In this example, we first take input from the user in the format of DD/MM/YYYY.
  • We then apply Zeller’s congruence formula to calculate the day of the week. This formula takes into account the day, month, and year of the given date.
  • The formula returns a number from 0 to 6, where 0 represents Saturday, 1 represents Sunday, and so on.
  • We then use a switch statement to print the day of the week based on the number returned by the formula.

Note: This program assumes that the input date is valid and does not perform any error checking.

Using Arrays and Loops

To better understand the program given below, it is recommended that you have prior knowledge of the following C Programming topics:

Here’s the C program that uses single and multidimensional arrays, for loop, and the string function strcpy() to find the day of the given date.


 
#include <stdio.h>
#include <string.h>
int main() {
// Define arrays for month names and day names
char months[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
char days[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// Prompt user to enter a date
int day, month, year;
printf("Enter a date (DD/MM/YYYY): ");
scanf("%d/%d/%d", &day, &month, &year);
// Determine if year is a leap year
int is_leap_year = 0;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
is_leap_year = 1;
}
} else {
is_leap_year = 1;
}
}
// Determine the number of days in each month
int days_in_month[12] = {31, 28 + is_leap_year, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// Calculate the day of the year
int day_of_year = day;
for (int i = 0; i < month - 1; i++) {
day_of_year += days_in_month[i];
}
// Calculate the day of the week using Zeller's congruence
int h = (day_of_year + ((13 * (month + 1)) / 5) + (year % 100) + ((year % 100) / 4) + ((year / 100) / 4) + (5 * (year / 100))) % 7;
// Copy the day name into a new string using strcpy()
char day_name[10];
strcpy(day_name, days[h]);
// Print the result
printf("%s %d, %d was a %s\n", months[month - 1], day, year, day_name);
return 0;
}
Copy code

Output

2023_10_image-3.jpg

Explanation:

In this example, we use 2D character arrays. 

  • The program prompts the user to enter a date in the format DD/MM/YYYY
  • Then, it extracts the day, month, and year from the input string. 
  • It then determines if the year is a leap year, calculates the number of days in each month, calculates the day of the year, and uses Zeller’s congruence to calculate the day of the week. 
  • Finally, it copies the day name into a new string using strcpy() and prints the result.

If you learned something new today and feel like exploring more in C, check out the best online C programming courses, only at Shiksha Online!

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio