Java Program to find if a year is a leap or not. Here, we see the various methods to find out whether a year is Leap or not in Java In 5 different ways. Soon Compiler is added so that you can execute the program yourself, along with suitable examples and sample outputs. The methods used in this case are:
-
- Using Ternary Operator.
- Using Static Method.
- Using Command Line Arguments.
- Using Function.
- Using If-Else.
A LEAP YEAR, also known as an intercalary year or a bissextile year is a calendar year containing 366 days, as compared to the regular 365 days a year.
The extra day is added to balance out the drift between an astronomical year and a seasonal year. The Earth’s revolution generally takes 365.25 days approx to complete. The extra day is added every four years to bridge that extra time period. Here’s an interesting link that explains why years 1800, 1900, 2100 are not Leap years, even though they are divisible by 4.
Using Ternary Operator
1) The ternary operator syntax is “if (condition)? value1:value2;”, if the condition is true then it returns value1, otherwise it returns value2.
2) We are calculating the given year is a leap year or not. A year divisible by 400 (or) a year divisible by 4 and not divisible by 100 is a leap year.
3) Read the year value using scanner object sc.nextLong and store it in the variable y.
In given example y=1948, y!=0, it is divisible by 400 so c=1 return to a, a is initialized as a=1. Then it prints 1948 is a leap year.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; class Leapyear { public static void main(String arg[]) { long a,y,c; Scanner sc=new Scanner(System.in); System.out.print("enter any calendar year :"); y=sc.nextLong(); if(y!=0) { a=(y%400==0)?(c=1):((y%100==0)?(c=0):((y%4==0)?(c=1):(c=0))); if(a==1) System.out.println(y+" is a leap year"); else System.out.println(y+" is not a leap year"); } else System.out.println("year zero does not exist "); } } |
Output:
1 2 |
enter any calendar year :1950 1950 is not a leap year |
1 2 |
enter any calendar year :1948 1948 is a leap year |
Using Static Method
1) In this program leapOrNot(long year) is the static method which finds the given year is a leap year or not. leapOrNot(long year) method calls at the main method of the class Leapyear. Then that static method starts the execution.
a) If the year divisible by 400 then it prints the given year is leap(or) If the given year divisible by 4 and not divisible by 100 then it prints the given year is a leap.
b) Otherwise, it prints “year is not a leap year”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Scanner; class Leapyear { public static void main(String arg[]) { long year; Scanner sc=new Scanner(System.in); System.out.print("enter any calendar year :"); year=sc.nextLong(); leapOrNot(year); } static void leapOrNot(long year) { if(year!=0) { if(year%400==0) System.out.println(year+" is a leap year"); else if(year%100==0) System.out.println(year+" is not a leap year"); else if(year%4==0) System.out.println(year+" is a leap year"); else System.out.println(year+" is not a leap year"); } else System.out.println("Year zero does not exist "); } } |
Output:
1 2 |
enter any calendar year :2016 2016 is a leap year |
Using Command Line Arguments
1) We can read command line arguments from “String args[]” of the main method.
2) Convert the year which is in the string format at arg[0], in to int using year=Integer.parseInt(arg[0]);. Integer is the wrapper class which converts the string into int data type.
a) If the year divisible by 400 then it prints the given year is leap or if the given year divisible by 4 and not divisible by 100 then it prints the given year is a leap.
b) Otherwise, it prints “year is not a leap year”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; class Leapyear { public static void main(String arg[]) { int year=Integer.parseInt(arg[0]); if(year!=0) { if(year%400==0) System.out.println(year+" is a leap year"); else if(year%100==0) System.out.println(year+" is not a leap year"); else if(year%4==0) System.out.println(year+" is a leap year"); else System.out.println(year+" is not a leap year"); } else System.out.println("Year zero does not exist "); } } |
Output:
1 2 |
>java Leapyear 2050 2050 is not a leap year |
Java Leap Year Or Not – Using Function
1) The leap(int year) function returns c=1 if the given year is a)divisible by 400 or b) divisible by 4 and not divisible by 100.
2) The function calls at the main method if year!=0, then it returns the c value and c value initialized to the variable “a” if a=1 then prints “is a leap year” otherwise prints “not a leap year”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
class Leapyear { int leap(int year) { int c=0; { if(year%400==0) c=1; else if(year%100!=0) { if(year%4==0) c=1; } } return c; } public static void main(String arg[]) { int y,a; Scanner sc=new Scanner(System.in); System.out.print("enter any calendar year :"); y=sc.nextInt(); Leapyear l=new Leapyear(); if(y!=0) { a=l.leap(y); if(a==1) System.out.println(y+" is a leap year"); else System.out.println(y+" is not a leap year"); } else System.out.println("year zero does not exist"); } } |
Output:
1 2 |
enter any calendar year :2016 2016 is a leap year |
Using If-Else
1) We are using if else condition to find the given year is a leap year or not.
2) The 1st if condition checks the given year is equal to zero or not, if not
a) If given year divisible by 400 it prints “is a leap year”, if condition false then come to else part.
b) If given year divisible by 100, then inner if condition checks the year is divisible by 4 or not if true then prints “is a leap year” otherwise prints “is not a leap year. If condition if(year%100!=0) is false then it prints “is not a leap year”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.Scanner; class Leapyear { public static void main(String arg[]) { int year; Scanner sc=new Scanner(System.in); System.out.print("enter any calendar year :"); year=sc.nextInt(); if(year!=0) { if(year%400==0) System.out.println(year+" is a leap year"); else if(year%100!=0) { if(year%4==0) System.out.println(year+" is a leap year"); else System.out.println(year+" is not a leap year"); } else System.out.println(year+" is not a leap year"); } else System.out.println("Year zero does not exist "); } } |
Output:
1 2 |
enter any calendar year :1999 1999 is not a leap year |
More Java Programs: