Java program to check leap year


Java program to check if a given year is leap year

A leap year is a year with an extra day. Leap year contains 366 days instead of 365. This extra day is added at the end of february so in a leap year february has 29 days instead of 28 days.
A year is a leap year if this condition is satisfied:
The year is divisible by 4 and not divisible by 100 or the year is divisible by 400.
We use the remainder operator % to check the divisibility of the year. 
The Java conditional statement to check whether a year is a leap year is this:
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
Here is the complete Java program to check leap year:
import java.util.Scanner;
public class LeapYear {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year;
        System.out.print("Enter year: ");
        year = input.nextInt();
        if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){                                                  
            System.out.println(year + " is a leap year");
        }else {
            System.out.println(year + " is not a leap year");
        }
       
    }
}
Sample output 1:
Enter year: 2000
2000 is a leap year
Sample output 2:
Enter year: 2013
2013 is not a leap year

No comments :

Post a Comment