Java Program to Check a year is a leap year or not

In this post, will write a java program to check whether the input year is a leap year or not. To determine whether a year is a leap year we will use if-else statements. And check if the year is divisible by 4, the year is divisible by 400 but not divisible by 100 than it is a leap year.

How the program will work?

  • If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
  • If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
  • If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
  • The year is a leap year (365 days)
  • The year is not a leap year. (366 days)Source of these steps.

Lets write this logic in java Program.

Example:

  1. Input1:
  2. Enter the year: 2016
  3. Output1:
  4. 2016 is a Leap Year.
  1. Input2:
  2. Enter the year: 2001
  3. Output2:
  4. 2016 is not a Leap Year.

Program to Check Leap Year

In this program, we will ask the user to enter the Year, and based on the user input value, the program will check if the year is divisible by 4 and if true then the check year is divisible by 100, and if it is false than is a leap year. And year which is divisible by 400 is also a leap year.

import java.util.Scanner;
public class leap_year {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the Year: ");
        int year = sc.nextInt();

        if((year%400==0) || ((year%4==0) && (year%100!=0))){
            System.out.println(year+" is a Leap Year.");
        }
        else {
            System.out.println(year+" is not a Leap Year.");
        }
    }
}

Output:

Enter the Year: 2016
2016 is a Leap Year.

Recommended:

  1. Java Program to solve quadratic equations
  2. Java Program to find the greatest of three Numbers
  3. Java Program to Display the weekday between 1 and 7
  4. Java Program to Find the number of days in a Months.
  5. Java Program to Print character is vowel or Consonant
  6. Java program Fill a bucket with water using a mug
  7. Java Program to Check if Number is Positive or Negative
  8.  

Leave a Comment

Your email address will not be published. Required fields are marked *

x