Java Code – Check Leap Year

By | July 12, 2019

Task: Java Code – Check Leap Year. Write a Java Program to input a year like 2018 and check whether it is a leap year or not.

The Source Code for Java Program to Check Leap Year

/*
 * Write a Program in Java to input a year
and checks whether it is a leay year or not
 */
package leapyear;

import java.util.Scanner;

/**
 * @author www.EasyCodeBook.com
 */
public class LeapYear {

    public static void main(String[] args) {

        int year;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a year to check for Leap Year?");
        year = input.nextInt();
        if ( ((year % 4 == 0) && !(year % 100 == 0))|| (year % 400 == 0) ) 
            System.out.println(year+" is a Leap Yaer"); 
       else 
            System.out.println(year+" is not a Leap Yaer"); 
    }
    
}

The output of Sample Run of the Java program

Enter a year to check for Leap Year?2020
2020 is a Leap Yaer

2nd Sample Run:
Enter a year to check for Leap Year?2022
2022 is not a Leap Yaer

Loading

Leave a Reply

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