;

Java Program to Check Whether the Year is a Leap Year or Not


Tutorialsrack 19/04/2021 Java

In this Java program, you’ll learn how to check whether the year is a leap year or not in Java.

What is a Leap Year?

A leap year is a calendar year that contains an additional day added to keep the calendar year synchronized with the astronomical year or seasonal year. Leap year is a year that is exactly divisible by four, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. For example, the years  2007, 2009, 2011 do not leap years but the years 1600, 2000, and 2020 are the leap years.

Java Program to Check Whether the Year is a Leap Year or Not

Java Program to Check Whether the Year is a Leap Year or Not
//Java Program to Check Whether the Year is a Leap Year or Not

import java.util.Scanner;

public class JavaPrograms {

	public static void main(String[] args) {

		int year;
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter any Year:");
		year = scanner.nextInt();
		scanner.close();
		boolean isLeap = false;

		if (year % 4 == 0) {
			if (year % 100 == 0) {
				if (year % 400 == 0)
					isLeap = true;
				else
					isLeap = false;
			} else
				isLeap = true;
		} else {
			isLeap = false;
		}

		if (isLeap == true) {
			System.out.println(year + " is a Leap Year.");
		} else {
			System.out.println(year + " is not a Leap Year.");
		}
	}
}
Output

Enter any Year:

1991

1991 is not a Leap Year.

 

Enter any Year:

2016

2016 is a Leap Year.


Related Posts



Comments

Recent Posts
Tags