Skip to content
Programming Exercises · Q4

Q.Write a program to check if the year entered by the user is a leap year or not.

Madhya Pradesh MpbseTextbookSubjective· 3mImportance★★★★★est
85% · 33/39 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Apply the calendar rule with one boolean expression — (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) — and print the verdict via if–else.

The idea: the Gregorian leap-year rule. A year is a leap year when it is divisible by 4, except century years (divisible by 100), which are leap only when divisible by 400. This three-part rule collapses into a single boolean condition using the modulo operator % (remainder), and the two-way outcome maps to if–else:

  • year % 4 == 0 and year % 100 != 0 → ordinary leap year (2024, 2016)
  • year % 400 == 0 → century leap year (2000, 1600)
  • Everything else → not a leap year (1900, 2023)

Program

year = int(input("Enter a year: "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print(year, "is a leap year")
else:
    print(year, "is not a leap year")

Sample runs

Enter a year: 2024
2024 is a leap year
Enter a year: 1900
1900 is not a leap year

Verifying the tricky cases

| Year | % 4 == 0 | % 100 != 0 | % 400 == 0 | Verdict |

| --- | --- | --- | --- | --- | …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.