Skip to content
CodeKruPro

Check whether a year is a leap year

Coding problemEasyAsked in: Infosys

Given a year, return true if it is a leap year in the Gregorian calendar (the calendar most countries use), and false otherwise.

The rules: a year divisible by 4 is a leap year. But if it is also divisible by 100, it is not. But if it is also divisible by 400, it is a leap year again. So 2024 and 2000 are leap years; 2023 and 1900 are not. Apply the rules to every year in the range.

Examples

Example 1
Input: year = 2024
Output: true
Example 2
Input: year = 1900
Output: false
Divisible by 100 but not by 400.
Example 3
Input: year = 2000
Output: true
Divisible by 400.

Constraints

  • year is between 1 and 1,000,000

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in