Loading...
Loading...
A calendar printing shop needs to know how many of the years from from to to (both included) are leap years, because those calendars need a 29 February page.
A year is a leap year when it is divisible by 4, except years divisible by 100, which are leap years only when also divisible by 400. Write a method static boolean isLeapYear(int year) and use it in a loop in main.
from to to, both included.Example 1
Input
2020 2030
Output
3
The leap years are 2020, 2024 and 2028.
Example 2
Input
1896 1904
Output
2
1900 is divisible by 100 but not by 400, so only 1896 and 1904 count.
Try the problem first. Open one hint at a time.
Check the rules from the most special to the most common: divisible by 400, then by 100, then by 4.
Or in one line: (year % 4 == 0 && year % 100 != 0) || year % 400 == 0.
In main, loop from from to to and add 1 to a counter whenever isLeapYear returns true.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor