Check leap year in Java
if else - Question 4
In this question, we will see how to check if a year is a leap year or not in Java programming using the if else statement. To know more about if else statement click on the if else statement lesson.
Q4) Write a program in Java to input a year and check if it is a leap year or not. A leap year is a year which is either divisible by 400 or it is not divisible by 100 but divisible by 4. Make use of logical operators (&& ||) to solve this question.
Program
import java.util.Scanner;
public class Q4
{
public static void main(String args[])
{
int y;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a year ");
y=sc.nextInt();
if(y%400==0 || (y%100!=0 && y%4==0))
{
System.out.print("Leap year");
}
else
{
System.out.print("Not leap year");
}
}
}
Output
Enter a year 2020 Leap year