logo image

Java program to take a year as input and tells whether it's a leap year or not



Following program shows you how take a year as input and tells whether it's a leap year or not.

import java.util.Scanner;

public class BasicMath5 {

	public static void main(String[] args) {

		System.out.println("Enter a year:");

		Scanner in = new Scanner(System.in);
		int input = in.nextInt();

		if (input % 4 == 0) {

			if (input % 100 == 0) {

				if (input % 400 == 0) {
					System.out.println(input + " its a leap year");

				} else {
					System.out.println(input + " its not a leap year");
				}

			} else {
				System.out.println(input + " its a leap year");

			}
		} else {
			System.out.println(input + " its not a leap year");

		}

	}

}

Output:

Example1:

Enter a year:
1980
1980 its a leap year

Example2:

Enter a year:
2001
2001 its not a leap year