Tutorialspoint
Problem
Solution
Submissions

Check Leap Year

Certification: Basic Level Accuracy: 0% Submissions: 0 Points: 8

Write a JavaScript program to determine if a given year is a leap year. A leap year is a year that is evenly divisible by 4, except for century years (years ending in 00) which must be divisible by 400 to be a leap year.

Example 1
  • Input: year = 2024
  • Output: true
  • Explanation:
    2024 is divisible by 4 (2024 ÷ 4 = 506). 2024 is not a century year (doesn't end in 00). Since it's divisible by 4 and not a century year, 2024 is a leap year.
Example 2
  • Input: year = 1900
  • Output: false
  • Explanation: 1900 is a century year (ends in 00). For century years, it must be divisible by 400 to be a leap year. 1900 ÷ 400 = 4.75, so it's not divisible by 400. Therefore, 1900 is not a leap year.
Constraints
  • 1 ≤ year ≤ 3000
  • The input will always be a positive integer
  • Consider both regular years and century years
  • Time Complexity: O(1)
  • Space Complexity: O(1)
NumberControl StructuresPwCArctwist
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Check if the year is divisible by 400 first - if yes, it's a leap year
  • If not divisible by 400, check if it's divisible by 100 - if yes, it's not a leap year
  • If not divisible by 100, check if it's divisible by 4 - if yes, it's a leap year
  • Use the modulo operator (%) to check divisibility
  • The order of conditions matters: check 400 first, then 100, then 4

Steps to solve by this approach:

 Step 1: First check if the year is divisible by 400 using the modulo operator.
 Step 2: If divisible by 400, return true immediately as it's definitely a leap year.
 Step 3: If not divisible by 400, check if the year is divisible by 100.
 Step 4: If divisible by 100 (but not 400), return false as century years need to be divisible by 400.
 Step 5: If not divisible by 100, check if the year is divisible by 4.
 Step 6: If divisible by 4, return true as non-century years divisible by 4 are leap years.
 Step 7: If none of the above conditions are met, return false as it's not a leap year.

Submitted Code :