Zeller\'s congruence is an algorithm developed by Christian Zeller to calculate
ID: 3873330 • Letter: Z
Question
Zeller's congruence is an algorithm developed by Christian Zeller to calculate the day of the week. The formula is h = ( q + (26(m + 1 ) / 10 )) + k + (k/4) + (j /4) + (5j) ) % 7
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 year / 100
k is the year of the century (i. e year % 100)
Note all divisions in this excerise perform 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.
Example from book:
Enter year (e.g 2012) : 2015
Enter month 1 -12 : 1
Enter the day of the month: 1 - 31: 25
Day of the week is Sunday.
Here is my work. I think I am not using remainder correctly.
import java.util.Scanner;
public class DayOfTheWeek
{
public static void main(String[] arg)
{
// Create a scanner
Scanner input = new Scanner(System.in);
// Print out Enter the year
System.out.println("Enter the year (e.g., 2012): ");
double year = input.nextDouble();
// Print out Enter month
System.out.println("Enter month 1-12: ");
double month = input.nextDouble();
// Print out enter the day of month
System.out.println("Enter the day of the month: 1- 31 ");
double day = input.nextDouble();
// last part of formula from the right side to left ( formlast adds l for each left)
double formlast = 5 * (year / 100);
double formlastl = year / 4;
double formlastll = (year % 100)/ 4;
double k = year % 100;
// bigthree is the division of the three including month
double bigthree = ((26 * ( month + 1)) / 10);
// add together to get h
double h = (day + bigthree + k + formlastll + formlastl + formlast) % 7;
System.out.println(h + " is the answer ");
// if statement for day
if ( h >= 0 && h < 1 )
System.out.println("Day of the week is Saturday! ");
else if ( h >= 1 && h < 2 )
System.out.println("Day of the week is Sunday! ");
else if (h >= 2 && h < 3 )
System.out.println("Day of the week is Monday! ");
else if (h >= 4 && h < 5 )
System.out.println("Day of the week is Teusday! ");
else if (h >= 6 && h < 7 )
System.out.println("Day of the week is Wednesday! ");
else if (h >= 8 && h < 9 )
System.out.println("Day of the week is Thursday! ");
else if (h >= 9 && h < 10 )
System.out.println("Day of the week is Friday! ");
else
System.out.println("Check and type again! ");
}
}
Explanation / Answer
// I have modified your program please see below code. which gives proper result now. Please upvote if you are satisifed .
Updated Answer Below with the fix of january month:
import java.util.Scanner;
public class DayOfTheWeek
{
public static void main(String[] arg)
{
// Create a scanner
Scanner input = new Scanner(System.in);
// Print out Enter the year
System.out.println("Enter the year (e.g., 2012): ");
int year = input.nextInt();
// Print out Enter month
System.out.println("Enter month 1-12: ");
int month = input.nextInt();
if (month == 1)
{
month = 13;
year--;
}
if (month == 2)
{
month = 14;
year--;
}
// Print out enter the day of month
System.out.println("Enter the day of the month: 1- 31 ");
int day = input.nextInt();
// last part of formula from the right side to left ( formlast adds l for each left)
int j = year/100;
int formlast = 5 * j;
int formlastl = j / 4;
int k = year % 100;
int formlastll = k/ 4;
// bigthree is the division of the three including month
int bigthree = ((26 * ( month + 1)) / 10);
// add together to get h
int h = (day + bigthree + k + formlastll + formlastl + formlast) % 7;
System.out.println(h + " is the answer ");
// if statement for day
if ( h == 0 )
System.out.println("Day of the week is Saturday! ");
else if ( h == 1 )
System.out.println("Day of the week is Sunday! ");
else if (h == 2 )
System.out.println("Day of the week is Monday! ");
else if (h == 3 )
System.out.println("Day of the week is Teusday! ");
else if (h == 4 )
System.out.println("Day of the week is Wednesday! ");
else if (h ==5 )
System.out.println("Day of the week is Thursday! ");
else if (h ==6 )
System.out.println("Day of the week is Friday! ");
else
System.out.println("Check and type again! ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.