Exercise: Leap Years

Leap years have an extra day in February, making it 29 days long instead of the usual 28. A leap year occurs mostly every four years. The rules are:

  1. A year is a leap year if it is divisible by 4.
  2. However, if the year is divisible by 100, it is not a leap year.
  3. But if the year is also divisible by 400, it is a leap year again.

Your program should print the leap years between a start and an end year, inclusive.

For example (user input in green):

Welcome to the leap year calculator 📅 🦘
Enter start year: 2000
Enter end year: 2050
Leap years: 2000 2004 2008 2012 2016 2020 2024 2028 2032 2036 2040 2044 2048
There are 13 leap years between 2000 and 2050

Another example:

Welcome to the leap year calculator 📅 🦘
Enter start year: 1599
Enter end year: 1602
Leap years: 1600
There is 1 leap year between 1599 and 1602

Hint 💡

You can check if a number is evenly divisible by another number using the remainder operator %. For example, year % 4 == 0 checks if year divided by 4 has remainder 0 and is thus evenly divisible.

Hand in instructions

  1. Make sure your program runs correctly.
  2. Hand in your program by uploading Main.java to Moodle.