Java: Check whether an year (integer) entered by the user is a leap year or not
Check Leap Year
Write a Java method to check whether an year (integer) entered by the user is a leap year or not.
Pictorial Presentation:

Sample Solution:
Java Code:
import java.util.Scanner;
public class Exercise10 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input a year: ");
int year = in.nextInt();
System.out.println(is_LeapYear(year));
}
public static boolean is_LeapYear(int y)
{
boolean a = (y % 4) == 0;
boolean b = (y % 100) != 0;
boolean c = ((y % 100 == 0) && (y % 400 == 0));
return a && (b || c);
}
}
Sample Output:
Input a year: 2017 false
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program to check if a year is a leap year and then determine the next leap year.
- Write a Java program to validate multiple years provided as a comma-separated string to check for leap years.
- Write a Java program to check if a year is a leap year using bitwise operations.
- Write a Java program to input a range of years and output all the leap years within that range.
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java method to print characters between two characters (i.e. A to P ).
Next: Write a Java method to check whether a string is a valid password.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics