Questions
Write a python program to check whether a given year is a Leap year or not.
Solution:
year=int(input("Enter any year"))
if (year%400==0) or (year%4==0 and year%100!=0):
print("leap year")
else:
print("Entered year is not Leap year")
OUTPUT:
Enter any year 2024
leap year
Explaination:
Input:
year = int(input("Enter any year"))
The code prompts the user to enter a year, which is then converted from a string to an integer and stored in the variable year.
Leap Year Logic: The condition for a year to be a leap year is defined as follows:
A year is a leap year if:
It is divisible by 400, or
It is divisible by 4 but not divisible by 100.
This is implemented in the following line:
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
year % 400 == 0: Checks if the year is divisible by 400.
year % 4 == 0 and year % 100 != 0: Checks if the year is divisible by 4 but not by 100.
Output: If the condition for a leap year is met, the program prints:
print("leap year")
Otherwise, it prints:
print("Entered year is not Leap year")
Report an error
Meanwhile you can watch this video
Watch Video