Java Program to Check Whether a Given Year is a Leap Year

Leap Year Program in Java: A year is a Leap Year if it satisfies the following conditions:

  • The year is exactly divisible by 400 (such as 2000,2400) or,
  • The year is exactly divisible by 4 (such as 2008, 2012, 2016) and not a multiple of 100 (such as 1900, 2100, 2200).
Problem Description

Write a Java Program to check whether a given year is a leap year or not.

Problem Solution

1. Take a year as input.
2. Check whether a given year is divisible by 400.
3. Check whether a given year is divisible by 100.
4. Check whether a given year is divisible by 4.
5. If the condition at step 2 and 4 becomes true, then the year is a leap year.
6. If the condition at step 3 becomes true, then the year is not a leap year.

Program/Source Code

Here is the source code of the Java Program to Find if a Given Year is a Leap Year. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

import java.util.Scanner;
public class Check_Leap_Year 
{
    public static void main(String args[])
    {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter any year:");
        int year = s.nextInt();
        boolean flag = false;
        if(year % 400 == 0)
        {
            flag = true;
        }
        else if (year % 100 == 0)
        {
            flag = false;
        }
        else if(year % 4 == 0)
        {
            flag = true;
        }
        else
        {
            flag = false;
        }
        if(flag)
        {
            System.out.println("Year "+year+" is a Leap Year");
        }
        else
        {
            System.out.println("Year "+year+" is not a Leap Year");
        }
    }
}
Program Explanation

1. Enter any year as an input. We first whether the given year is divisible by 400 or not.
2. If it is divisible then it is a leap year else we check for further conditions.
3. Now if it is divisible by 100 then it is not a leap year or else we further divide it by 4.
4. If it is divisible then it is a leap year else its not.

advertisement
advertisement
Runtime Testcases

Testcase 1: In this case, we enter the year “1800” as input to check whether a given year is leap year or not.

Enter any year:
1800
1800 is not a Leap Year

Testcase 2: In this case, we enter the year “2000” as input to check whether a given year is leap year or not.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Enter any year:
2000
2000 is a Leap Year

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.