JAVA PROGRAM TO CHECK LEAP YEAR

LEAP YEAR: A leap year is a year which occurs every four years, while an ordinary year has 365days, a leap year has 366days, the extra day is added to the month of february making it 29days.

We have to follow certain steps to determine whether an input year is a leap year or not :

  • 1. If the input year is divisible by 400 then its a leap year, if not then we check for the othe conditions.

  • 2. Now if the year is divisible by 100, then its not a leap year or else we check for one more condition.

  • 3. We divide the input year by 4, if its divisible then the given year is a leap year else its not a leap year.

Example 1 : java Program to Find the Leap Year

copyimage
//Java Program to check if a given year is a leap year or not
import java.util.Scanner;
class LeapYearDemo{
 public boolean findleapyear(int year){
	 
    if(year%400==0) {
       return true;
    }
    else if(year%100==0) {
       return false;
    }
    else if(year%4==0) {
       return true;
    }
    else return false;
  }
}
public class Codebator{
 public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter Year");
    int year = sc.nextInt();
    LeapYearDemo obj=new LeapYearDemo();
    boolean result=obj.findleapyear(year);
    if(result) {
    	System.out.println("The Input Year is a Leap Year");
    }
    else {
    	System.out.println("The Input Year is not a Leap Year");
    }
 }
}

OUTPUT :

Enter any number1800 The Input Year is not a Leap Year 

Example 2 : java Program to Find the Leap Year(another way)

copyimage
//Java Program to check if a given year is a leap year or not
import java.util.Scanner;
class LeapYearDemo{
 public boolean findleapyear(int year){
	 
    if(year % 4 == 0)
     {
         if( year % 100 == 0)
         {
             if ( year % 400 == 0)
                 return true;
             else
                 return false;
         }
         else
             return true;
     }
     else return false;
  }
}
public class Codebator{
 public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter Year");
    int year = sc.nextInt();
    LeapYearDemo obj=new LeapYearDemo();
    boolean result=obj.findleapyear(year);
    if(result) {
    	System.out.println("The Input Year is a Leap Year");
    }
    else {
    	System.out.println("The Input Year is not a Leap Year");
    }
 }
}

OUTPUT :

Enter any number1800 The Input Year is not a Leap Year 

Some useful links :