Algorithm


  1. Input the year to be checked.

  2. If the year is evenly divisible by 4, go to the next step. If not, the year is not a leap year.

  3. If the year is divisible by 4, but not by 100, go to the next step. If the year is divisible by both 4 and 100, go to step 4.

  4. If the year is divisible by 100 but not divisible by 400, then the year is not a leap year. If the year is divisible by both 100 and 400, go to the next step.

  5. If the year is divisible by 400, then it is a leap year. Otherwise, it is not.

 

Code Examples

#1 Code Example- Java Program to Check a Leap Year

Code - Java Programming

public class Main {

  public static void main(String[] args) {

    // year to be checked
    int year = 1900;
    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.");
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
1900 is not a leap year.
Advertisements

Demonstration


Java Programing to Check Leap Year-DevsEnv