Check whether given year is Leap year or not

leap year in the Gregorian calendar has an extra day for February. A leap year has 366 days instead the 365 days of a common year. Leap year is a multiple of ‘4’. Leap year has 29 days for February. Normal year has 28 days for February
Leap year: 366 days, 52 weeks + 2 odd days
Non-Leap year: 365 days, 52 weeks + 1 odd day

The standard logic to determine whether a year is leap year or not is: If the year is divisible by 4 and is not divisible by 100 or the year is divisible by 400 then it’s a leap year. 

PROGRAM
class LeapYear {

 public static void main(String[] args) {

  int year = Integer.parseInt(args[0]);
   
        boolean isLeapYear = false;
 
        if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) 
        {
         System.out.println("Year "+year+" is a Leap Year");
        }
        else 
        {
         System.out.println("Year "+year+" is not a Leap Year");
        }
 }
}
OUTPUT 1
C:\>javac LeapYear.java
C:\>java LeapYear 2000
Year 2000 is a Leap Year
OUTPUT 2
C:\>javac LeapYear.java
C:\>java LeapYear 2015
Year 2015 is not a Leap Year

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative