This post is about how to write a java program or code for finding out the year is a leap year or not. Leap year is a year in which February has only 28 days in a month and it comes after a gap of every 4 years.
For writing a java program t find out the year is a leap year or not required basic concepts of conditional statements especially concepts related to the "if-else". Let's begin to write code.
Test Condition
Input the year: 2008
2008 is a leap year
Objective:
Write a Java program that takes a year from the user and prints whether that year is a leap year or not.
Code
public class LeapYearOrNot {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input the year: ");
int year = in.nextInt();
boolean x = (year % 4) == 0;
boolean y = (year % 100) != 0;
boolean z = ((year % 100 == 0) && (year % 400 == 0));
if (x && (y || z))
{
System.out.println(year + " is a leap year");
}
else
{
System.out.println(year + " is not a leap year");
}
}
}
Output
Input the year: 2008
2008 is a leap year
0 Comments