Algorithm


  1. Input:

    • Accept the year as input.
  2. 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.
  3. Check for Divisibility by 100:

    • If the year is divisible by 100, go to the next step; otherwise, it's a leap year.
  4. Check for Divisibility by 400:

    • If the year is divisible by 400, it's a leap year; otherwise, it's not.
  5. 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

x
+
cmd
Enter a year: 2000
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

x
+
cmd
Enter a year: 2000
2000 is a leap year
Advertisements

Demonstration


JavaScript Programing Example to Check Leap Year-DevsEnv

Previous
JavaScript Practice Example #3 - Assign 3 Variables and Print Good Way