Saturday, September 16, 2023

 Check if a given year is a leap year



A leap year is a year with 366 days which is 1 extra day than a normal year.  This extra day comes in the month of February and on leap year Feb month has 29 days than normal 28 days. 
A leap year comes in an interval of 4 years.

A year is a leap year if the following conditions are satisfied: 
  1. The year is a multiple of 4
  2.  The year is a multiple of 400 and not a multiple of 100.


Here is a simple Java program for checking if a given year is a leap year:


public class DevPrimer {

public static void main(String[] args) {
int year = 2012; // Replace this with the year you want to check

if (isLeapYear(year)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}

public static boolean isLeapYear(int year) {
return year % 4 == 0 && (year % 400 == 0 || year % 100 != 0);
}
}


That's all on how to check if a given year is a leap year in Java.



No comments:

Post a Comment