felix715 / C-Calendar

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Zeller's Congruence is an algorithm that calculates the day of the week for a given date. It was devised by Christian Zeller in the late 19th century and is a mathematical formula that can determine the day of the week for any date in history. The algorithm takes into account the day, month, and year of the date.

Here's a breakdown of Zeller's Congruence algorithm:

Month Adjustment: In the algorithm, January and February are treated as months 13 and 14 of the previous year. So, if you're working with January or February, you decrement the year by 1 and add 12 to the month.

Year Splitting: Divide the year into two parts: the last two digits and the first two digits. For example, for the year 2023, you have k = 23 and j = 20.

Day Calculation: Calculate a day value (h) using the following formula:

sql Copy code h = (day + 13 * (month + 1) / 5 + k + k / 4 + j / 4 - 2 * j) % 7 day: The day of the month (1 to 31). month: The adjusted month (3 to 14). k: The last two digits of the year. j: The first two digits of the year. Day Mapping: Map the result h to a day of the week:

0: Saturday 1: Sunday 2: Monday 3: Tuesday 4: Wednesday 5: Thursday 6: Friday The algorithm calculates h, which represents the day of the week, with 0 corresponding to Saturday, 1 to Sunday, and so on. The result h tells you the day of the week for the given date.

The purpose of this algorithm is to provide a way to determine the day of the week algorithmically without relying on precomputed tables or external data. It's a formulaic approach that can be used to calculate the day of the week for any date, past or future.

In the C++ code given, Zeller's Congruence is used to calculate the day of the week for the first day of each month in a given year, which is then used to format and print the calendar.

About