Algorithm
-
Input:
- Accept the year as input.
-
Check for Divisibility by 4:
- Check if the year is divisible by 4. If yes, go to the next step; otherwise, it's not a leap year.
-
Check for Divisibility by 100:
- If the year is divisible by 100, go to the next step; otherwise, it's a leap year.
-
Check for Divisibility by 400:
- If the year is divisible by 400, it's a leap year; otherwise, it's not.
-
Output:
- If the year satisfies all the conditions, output that it is a leap year; otherwise, output that it is not a leap year.
Code Examples
#1 Code Example- Check Leap Year Using if...else
Code -
Javascript Programming
// program to check leap year
function checkLeapYear(year) {
//three conditions to find out the leap year
if ((0 == year % 4) && (0 != year % 100) || (0 == year % 400)) {
console.log(year + ' is a leap year');
} else {
console.log(year + ' is not a leap year');
}
}
// take input
const year = prompt('Enter a year:');
checkLeapYear(year);
Copy The Code &
Try With Live Editor
Output
Enter a year: 2000
2000 is a leap year
2000 is a leap year
#2 Code Example- Check Leap Year Using newDate()
Code -
Javascript Programming
// program to check leap year
function checkLeapYear(year) {
const leap = new Date(year, 1, 29).getDate() === 29;
if (leap) {
console.log(year + ' is a leap year');
} else {
console.log(year + ' is not a leap year');
}
}
// take input
const year = prompt('Enter a year:');
checkLeapYear(year);
Copy The Code &
Try With Live Editor
Output
Enter a year: 2000
2000 is a leap year
2000 is a leap year
Demonstration
JavaScript Programing Example to Check Leap Year-DevsEnv