Skip to content

Commit 97c634f

Browse files
finished Exercise27
1 parent eb3c4c0 commit 97c634f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @author : Jonathan Birkey
3+
* @mailto : [email protected]
4+
* @created : 27Dec2021
5+
* <p>(Display leap years) Write a program that displays all the leap years, 10 per line, from
6+
* 101 to 2100, separated by exactly one space. Also display the number of leap years in this
7+
* period.
8+
*/
9+
package com.github.jonathanbirkey.chapter05;
10+
11+
public class Exercise27 {
12+
public static void main(String[] args) {
13+
int yearsPerLine = 10;
14+
int printedYears = 0;
15+
int startYear = 101;
16+
int endYear = 2100;
17+
int totalLeapYears = 0;
18+
19+
for (int i = startYear; i <= endYear; i++) {
20+
if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) {
21+
printedYears++;
22+
if (printedYears == yearsPerLine) {
23+
System.out.printf("%d\n", i);
24+
printedYears = 0;
25+
} else {
26+
System.out.printf("%d ", i);
27+
}
28+
totalLeapYears++;
29+
}
30+
}
31+
System.out.printf("\nNumber of leap years in this period: %d", totalLeapYears);
32+
}
33+
}

0 commit comments

Comments
 (0)