Java program to check leap year

Write a java program to check leap year

Leap year contains 366 days, which comes once every four years.
Every leap year corresponds to these facts:

  • A century year is a year ending with 00.  A century year is a leap year only if it is divisible by 400.
  • A leap year (except a century year) can be identified if it is exactly divisible by 4.
  • A century year should be divisible by 4 and 100 both.
  • A non-century year should be divisible only by 4.

Let's find out whether a year is a leap year or not

  • Program:

public class Main {

  public static void main(String[] args) {

    // year to be checked
    int year = 2011;
    boolean leap = false;

    // if the year is divided by 4
    if (year % 4 == 0) {

      // if the year is century
      if (year % 100 == 0) {

        // if year is divided by 400
        // then it is a leap year
        if (year % 400 == 0)
          leap = true;
        else
          leap = false;
      }
      
      // if the year is not century
      else
        leap = true;
    }
    
    else
      leap = false;

    if (leap)
      System.out.println(year + " is a leap year.");
    else
      System.out.println(year + " is not a leap year.");
  }
}

  • Output:

2011 is not a leap year

--- Another example ---

2012 is a leap year

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here 
Java program to find the largest among three numbers Here
Java program to find all roots of a quadratic equation Here

0 comments