Zeller’s congruence is an algorithm developed by Christian Zeller to calculate the day of the week. The formula is
where:
h is the day of the week (0: Saturday, 1: Sunday, 2: Monday, 3: Tuesday, 4: Wednesday, 5: Thursday, 6: Friday).
■ q is the day of the month.
■ m is the month (3: March, 4: April, …, 12: December). January and February are counted as months 13 and 14 of the previous year.
■ j is the century (i.e., year 100).
■ k is the year of the century (i.e., year % 100).
Note that the division in the formula performs an integer division. Write a program that prompts the user to enter a year, month, and day of the month, and displays the name of the day of the week.
Solution:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int year, month, dayOfMonth;
int dayOfWeek, century, yearOfCentury;
System.out.print("Enter year: (e.g., 2014):");
year = s.nextInt();
System.out.print("Enter month: 1-12:");
month = s.nextInt();
System.out.print("Enter the day of the month: 1-31:");
dayOfMonth = s.nextInt();
// testing if month is January or February
if(month == 1) {
month = 13;
} else if(month == 2) {
month = 14;
}
century = year / 100;
yearOfCentury = year % 100;
dayOfWeek = (dayOfMonth + ((26 * (month + 1))/10) + yearOfCentury + yearOfCentury/4 + century/4 + century * 5) % 7;
if(dayOfWeek == 0) {
System.out.print("Day of the week is Saturday.");
} else if(dayOfWeek == 1) {
System.out.print("Day of the week is Sunday.");
} else if(dayOfWeek == 2) {
System.out.print("Day of the week is Monday.");
} else if(dayOfWeek == 3) {
System.out.print("Day of the week is Tuesday.");
} else if(dayOfWeek == 4) {
System.out.print("Day of the week is Wednesday.");
} else if(dayOfWeek == 5) {
System.out.print("Day of the week is Thursday.");
} else if(dayOfWeek == 6) {
System.out.print("Day of the week is Friday.");
}
}