Java: Check whether an year (integer) entered by the user is a leap year or not
Java Method: Exercise-10 with Solution
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:
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.
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://w3resource.com/java-exercises/method/java-method-exercise-10.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics