Java program to check Leap year or not
1. Java program to check Leap year or not using if-else statement:
Program:
import java.io.*; import java.util.*; class LeapYear { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter the year"); int yr=sc.nextInt(); if((yr%400==0)||(yr%100!=0 && yr%4==0)) { System.out.println(yr+ " is a leap year"); } else { System.out.println(yr+" is not a leap year"); } } }
Output:
Enter the year 2000 2000 is a leap year
Enter the year 2001 2001 is not a leap year
2. Java program to check Leap year or not using Buffered Reader class:
Program:
import java.util.*; import java.lang.*; import java.io.*; class LeapYear { public static void main(String args[]) throws Exception { BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the year"); int yr= Integer.parseInt(br.readLine()); if((yr%400==0)||(yr%100!=0 && yr%4==0)) { System.out.println(yr+ " is a leap year"); } else { System.out.println(yr+" is not a leap year"); } } }
Output:
Enter the year 2001 2001 is not a leap year
3. Java program to check Leap year or not using Command Line Argument:
Program:
import java.util.*; class LeapYear { public static void main(String args[]) { int yr= Integer.parseInt(args[0]); System.out.println(yr); if((yr%400==0)||(yr%100!=0 && yr%4==0)) { System.out.println(yr+ " is a leap year"); } else { System.out.println(yr+" is not a leap year"); } } }
Compile:
javac LeapYear.java
Run:
java LeapYear 2004 2004 is a leap year