w3resource

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:

Java Method Exercises: Compute the sum of the digits in an integer

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:

Flowchart: Check whether an year (integer) entered by the user is a leap year or not


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.

Go to:


PREV : Print Characters Between Two Characters
NEXT : Validate Password.


Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.