-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEulerProblem0019.java
More file actions
31 lines (30 loc) · 922 Bytes
/
EulerProblem0019.java
File metadata and controls
31 lines (30 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//How many Sundays fell on the first of the month during the twentieth century
//(1 Jan 1901 to 31 Dec 2000)?
public static void main(String[] args) {
int monthDay = 0;
int weekDay = 0;
int month = 0;
int year = 0;
int sundays = 0;
while (year <= 100) {
if ((weekDay == 6) && (monthDay == 0) && (year > 0)) sundays++;
weekDay = (weekDay+1)%7;
if (month == 1) {
if ((year%4 != 0) || (year%400 == 0)) {
monthDay = (monthDay+1)%28;
}
else monthDay = (monthDay+1)%29;
}
else if ((month == 3) || (month == 5) || (month == 8) || (month == 10)) {
monthDay = (monthDay+1)%30;
}
else {
monthDay = (monthDay+1)%31;
}
if (monthDay == 0) {
month = (month+1)%12;
if (month == 0) year++;
}
}
System.out.println(sundays);
}