w3resource

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

Java Method: Exercise-10 with Solution

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

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/method/java-method-exercise-10.php