Question 9 of 13intermediate🔧 ApplyLong Answer4 marks

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

Correct Answer

python #Program to check if the year is a leap year or not year = int(input("Enter the year to be checked: "))

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

Explanation:

The program takes the year as input from the user using the int(input()) function, similar to the approach shown in Program 6-14 for checking prime numbers.

A year is a leap year if it is divisible by 4. However, if the year is divisible by 100, it is NOT a leap year unless it is also divisible by 400. This logic is implemented using if-elif-else statements.

The conditions are checked in order:

  1. If divisible by 400 → leap year
  2. Else if divisible by 100 → not a leap year
  3. Else if divisible by 4 → leap year
  4. Otherwise → not a leap year

The program then displays the appropriate output message indicating whether the entered year is a leap year or not.

Exercise: PROGRAMMING EXERCISES | Q: 4
For More Understanding

Explanation

This question is from the Programming Exercises section of the NCERT textbook. The answer follows the same programming style demonstrated in Program 6-14 (checking prime numbers), which uses int(input()) for user input, conditional statements for logic, and print statements for output. The leap year logic is a standard algorithm taught in Class 11 Computer Science.

Solution Steps

  1. Step 1: Accept year input from user using int(input())

  2. Step 2: Check if year is divisible by 400 (leap year)

  3. Step 3: Else check if divisible by 100 (not a leap year)

  4. Step 4: Else check if divisible by 4 (leap year)

  5. Step 5: Otherwise, not a leap year