Zeller's Congruence algorithm | Find the Day for a Date.

Zeller's Congruence algorithm is a mathematical formula that can be used to determine the day of the week for a given date. It was developed by Christian Zeller in the late 19th century and provides a straightforward calculation based on the day, month, and year.


The algorithm takes advantage of a modified calendar where January and February are considered months 13 and 14 of the previous year. This adjustment simplifies the calculation and allows for consistent formulas across all months.


The formula used in Zeller's Congruence algorithm is as follows:

h = (day + (13 * (month + 1)) / 5 + yearOfCentury + yearOfCentury / 4 + century / 4 + 5 * century) % 7

Where:
  • h is the calculated day of the week (0 - Saturday, 1 - Sunday, ..., 6 - Friday).
  • day is the day of the month (1 to 31).
  • month is the month of the year (3 to 14, with January and February adjusted as 13 and 14 respectively).
  • year is the year (e.g., 2023).
  • century is the century part of the year (e.g., 20 for the year 2023).
  • yearOfCentury is the year within the century (e.g., 23 for the year 2023).

The formula calculates a value of h, which represents the day of the week. To map this value to the corresponding day of the week, you can use the following mapping:
  • 0: Saturday
  • 1: Sunday
  • 2: Monday
  • 3: Tuesday
  • 4: Wednesday
  • 5: Thursday
  • 6: Friday

By applying Zeller's Congruence algorithm, you can determine the day of the week for any given date, enabling you to perform various date-related calculations and manipulations in programming.

C++ Program to Find the Day for a Date.

Example code:
//C++ Program Find the Day for a Date
#include <iostream>
using namespace std;

int getDayOfWeek(int day, int month, int year) {
    if (month == 1 || month == 2) {
        month += 12;
        year--;
    }

    int century = year / 100;
    int yearOfCentury = year % 100;

    int h = (day + (13 * (month + 1)) / 5 + yearOfCentury + yearOfCentury / 4 + century / 4 + 5 * century) % 7;

    return h;
}

int main() {
    int day, month, year;

    cout << "Enter the date (DD MM YYYY): ";
    cin >> day >> month >> year;

    int dayOfWeek = getDayOfWeek(day, month, year);

    switch (dayOfWeek) {
        case 0:
            cout << "Saturday\n";
            break;
        case 1:
            cout << "Sunday\n";
            break;
        case 2:
            cout << "Monday\n";
            break;
        case 3:
            cout << "Tuesday\n";
            break;
        case 4:
            cout << "Wednesday\n";
            break;
        case 5:
            cout << "Thursday\n";
            break;
        case 6:
            cout << "Friday\n";
            break;
        default:
            cout << "Invalid day of the week\n";
            break;
    }

    return 0;
}
Output:
Enter the date (DD MM YYYY): 12 06 2023
Monday

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS