Leap Year Check in Java: Explanation & Practice
Determine if a year is a leap year
Problem summary
Check if 2024 is a leap year using the proper rules (div by 4, unless div by 100 unless div by 400).
Starter code
public class Main {
public static void main(String[] args) {
int year = 2024;
// Check leap year rules
// Print: 2024 is a leap year: true/false
}
}Expected output and test cases
- 2024 is a leap year
2024 is a leap year: true
Hints
- Divisible by 4 is the first check
- If divisible by 100, must also be divisible by 400
- Use nested if statements or combine with && and ||
Related Control Flow exercises
- Practice Check Voting Age in Java
- Practice Grade Calculator in Java
- Practice Print Numbers 1-5 in Java
Practice all Control Flow exercises · Run this idea in the Java compiler