Here I’ll show you how to check whether a year is a leap year or not using PHP, Python, and JavaScript.
Programming logic to check leap year
- If a year is a century year (eg. 1900, 2000) and is divisible by 400, it is a leap year.
- If a year is not a century year (eg. 1988, 2004) and is divisible by 4, it is a leap year.
- All other years are non-leap years.
Implementing the leap year logic in programming languages
- JavaScript
- PHP
- Python
const year = 2024;
if ((year % 100 != 0 && year % 4 == 0) || year % 400 == 0) {
console.log(year + " is Leap Year.");
} else {
console.log(year + " is Non Leap Year.");
}
<?php
$year = 2004;
if (($year % 100 != 0 && $year % 4 == 0) || ($year % 400 == 0)) {
echo "$year is Leap Year.";
} else {
echo "$year is Non Leap Year";
}
year = 1981
if((year % 100 != 0 and year % 4 == 0) or (year % 400 == 0)):
print("{0} is Leap Year.".format(year))
else:
print("{0} is Non Leap Year.".format(year))