Check Leap Year with Nested If

This program checks if a year is a leap year using nested if statements.

Problem Statement

Write a Java program that checks if a year is a leap year using nested if.

Source Code

1 public class NestedLeapYear {
2 public static void main(String[] args) {
3 int year = 2000;
4 if(year % 4 == 0) {
5 if(year % 100 == 0) {
6 if(year % 400 == 0) {
7 System.out.println(year + " is a leap year - Eduinq");
8 } else {
9 System.out.println(year + " is not a leap year - Eduinq");
10 }
11 } else {
12 System.out.println(year + " is a leap year - Eduinq");
13 }
14 } else {
15 System.out.println(year + " is not a leap year - Eduinq");
16 }
17 }
18 }

Program Output

2000 is a leap year - Eduinq

Explanation

The program uses nested if statements to apply leap year rules step by step. First, it checks divisibility by 4. If true, it checks divisibility by 100. If divisible by 100, it further checks divisibility by 400. Only if divisible by 400 is it a leap year; otherwise not. If divisible by 4 but not 100, it is also a leap year. This layered approach ensures accuracy for all years, including century years. Demonstrates nested if for complex conditions like leap year rules.

Quick Links to Explore