Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Check if a Given Year is a Leap Year

Apply the Gregorian leap-year rule with conditions. Students trace ordinary years, century exceptions, and multiples of four hundred without date libraries.

BeginnerPhase 1 - Level 1: Simple ConditionsExample 5 of 10
check-if-a-given-year-is-a-leap-year.py
1# Take year as input
2year = int(input("Enter a year: "))
3
4# Check if leap year
5if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
6 print("Leap year")
7else:
8 print("Not a leap year")

Output

Enter a year: 2024
Leap year

Enter a year: 2023
Not a leap year

Enter a year: 2000
Leap year

In this one

  • Apply leap-year exceptions
  • Group conditions with parentheses
  • Test century boundaries

How it works

  1. 1

    Read the year as an integer.

  2. 2

    Check the noncentury multiple-of-four rule.

  3. 3

    Also accept multiples of four hundred.

Edge cases

Is 1900 a leap year?

Is 1900 a leap year?

No. It is a century year not divisible by 400.

Is 2000 a leap year?

Is 2000 a leap year?

Yes. The second part of the condition is true.

What's going on

A leap year needs a more careful rule than divisibility by four alone. Most multiples of four qualify, but century years need to be divisible by four hundred.

The parenthesized and group accepts multiples of four that are not multiples of one hundred. The or group restores years such as 2000. Thus 2024 and 2000 qualify, while 1900 and 2023 do not.

Is 1900 a leap year? No. It is a century year not divisible by 400.

Is 2000 a leap year? Yes. The second part of the condition is true.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why is divisibility by four not enough?

Pick an answer to check it.

Question 2

Is 1900 a leap year?

Pick an answer to check it.

Questions people ask

Is 1900 a leap year?

No. It is a century year not divisible by 400.

Is 2000 a leap year?

Yes. The second part of the condition is true.

Why is divisibility by four not enough?

Century years have an exception.