Write a program to check if the year entered by the user is a leap year or not.
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:
- If divisible by 400 → leap year
- Else if divisible by 100 → not a leap year
- Else if divisible by 4 → leap year
- Otherwise → not a leap year
The program then displays the appropriate output message indicating whether the entered year is a leap year or not.
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
Step 1: Accept year input from user using int(input())
Step 2: Check if year is divisible by 400 (leap year)
Step 3: Else check if divisible by 100 (not a leap year)
Step 4: Else check if divisible by 4 (leap year)
Step 5: Otherwise, not a leap year