Method 2: Using the Ternary Operator for Finding Leap Year
In the previous method, we can see that using if-else statements in our code makes it a lot longer. The ternary operators are used to reduce the length of the if-else statement in our code. Thus, making the code short and easy to understand.
Implementation
class Solution {
public static void main(String[] args) {
int year = 2023 ;
// Taking a boolean flag by defualt false
boolean flag = false;
// Checking the conditions and updating the flag
flag = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)? true: false;
// Printing the results
if (!flag){
System.out.println(year + " is not a Leap Year.");
}
else{
System.out.println(year + " is a Leap Year.");
}
}
}
You can also try this code with Online Java Compiler
Run Code
Output
2023 is not a Leap Year.
Explanation
In the above method, we used a boolean variable to store the condition for leap year returned by the ternary operations. If the conditions are satisfied, we will get a true value, and if not, then a false value. After getting the values, we check them using if-else conditions and print the result accordingly. This method reduced the length of code drastically compared to the previous method.
Also see, Java Ioexception
Method 3: Using the command line to check for leap year in Java
In Java, the command-line arguments are the arguments that are passed at the time of running the Java code. These arguments are passed from the console and are received in the Java program as input. Users can pass arguments during the execution of the program by passing these command-line arguments in the main() method.
Two methods to take command line input in Java:
- Using command-line arguments
- Using Scanner class
Using Command Line Arguments
In Java, command-line arguments are used to pass arguments to the main program. The Java main method accepts a String array as an argument. When we pass a command line argument, they are treated as a string. Thus if we want to check for a leap year, the string should be converted to an integer.
Let's look at the code to understand it better.
Implementation
public class HelloJava {
public static void main(String[] args) {
if (args.length != 0){
// converting string into integer
int year = Integer.parseInt(args[0]);
boolean flag = false ;
flag = (year % 4 == 0 && year % 100 !=0 || year % 400 == 0)? true : false ;
if(flag == false) {
System.out.println(year + " is not a Leap Year.");
}else {
System.out.println(year + " is a Leap Year.");
}
}else {
System.out.println("No arguments provided.");
}
}
}
You can also try this code with Online Java Compiler
Run Code
Output
2023 is not a Leap Year.
Explanation
In the above example, we used the Integer.parseInt() to convert the string argument into an integer. Now once the year is in integer format, we have applied various conditions to check for a leap year. After checking the conditions, we have printed the result accordingly.
Using Scanner Class
In Java, the Scanner class can be used to take command-line inputs from the user. It is present in the java.util package. Scanner class provides various methods like nextInt(), nextDouble(), etc., to take numerical input. Thus, the need for parsing using Integer.parseInt() is eliminated.
Let us now look at the code to check for a leap year using the Scanner class.
Implementation
import java.util.Scanner;
public class HelloJava {
public static void main(String[] args) {
// Creating a Scanner object
Scanner s = new Scanner(System.in);
System.out.println("Enter the year to check for a Leap Year: ");
// Taking user input from command-line
int year = s.nextInt();
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.");
}
}
}
You can also try this code with Online Java Compiler
Run Code
Output
Enter the year to check for a Leap Year: 2023
2023 is not a Leap Year.
Explanation
In the above code, we are taking a command-line input using the Scanner object from the user. We used the nextInt() method of the Scanner object to store the year entered by the user and used if-else conditions to check for a leap year.
Method 4: Using in-built isLeap() method to check for Leap Year in Java
Java has an in-built isLeap() method that is used to check whether an input year is leap or not.
Implementation
Java
import java.time.Year;
public class LeapYearExample {
public static void main(String[] args) {
int yearToCheck = 2024;
if (Year.of(yearToCheck).isLeap()) {
System.out.println(yearToCheck + " is a leap year.");
} else {
System.out.println(yearToCheck + " is not a leap year.");
}
}
}
You can also try this code with Online Java Compiler
Run Code
Output
2024 is a leap year.
Explanation
In code above, the java.time.Year class is used to create a Year object for the year you want to check. The isLeap() method of the Year class returns true if the year is a leap year and false otherwise.
Frequently Asked Questions
How to write leap year program in Java?
In order to check a leap year in Java, check whether the year is divisible by 400. If it is, then it is a leap year. Also, if the year is divisible by both 4 and 100, then it is also a leap year. Otherwise, it is not a leap year.
How do you define a leap year in Java?
A leap year is a year with an extra day, making it 366 days in a year. In Java, you can define a leap year using various methods that check for a leap year. These methods can be if-else conditions, ternary operators, etc.
Is 3000 a leap year or not?
A year that is divisible by 100 should also be divisible by 400 and 4 to be a leap year. Hence, 3000 is not a leap year because it is not divisible by 400.
What is an example of a leap year?
2024 is an example of a leap year as it is divisible by only 4 and not 100 or 400. According to the rules of the Gregorian calendar, a leap year number must be divisible by four – except for end-of-century years, which must also be divisible by 400.
Conclusion
In this article, we discussed the problem of determining whether a year is a leap year or not. We also saw the approach and Java code to solve it. Hope this article helped you to understand this question in an easy way and motivated you through your coding journey.
Check out other articles to learn more:
Refer to our Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc.
Happy Coding!