An example of a Java method for determining whether or not a leap year occurs

  • 2020-04-01 03:54:04
  • OfStack

import java.util.Scanner; 
public class LeapYear { 
  public static void main(String[] arg){ 
    Scanner scan = new Scanner(System.in); 
    System.out.println(" Please enter a year :"); 
    long year= scan.nextLong(); 
    if(year%4==0 && year%100 !=0 ||year%400 ==0){ 
      System.out.println(year +" Is a leap year "); 
    } 
    else{ 
      System.out.println(year +" Not a leap year !"); 
    } 
  } 
} 

Conclusion:
The key is to determine the formula for calculating years. Leap years must satisfy two conditions: first, they must be divisible by 4, but not by 100. Number two: divisible by 400
The Scanner class is used to receive input.


Related articles: