year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("It's a leap year!")
else:
print("It's not a leap year.")
#source code --> clcoding.com
Code Explanation:
Taking Input from the User
year = int(input("Enter a year: "))
The program asks the user to enter a year.
Since input() takes input as a string, we use int() to convert it into an integer.
The value is stored in the variable year.
Checking if the Year is a Leap Year
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
A year is a leap year if one of the following conditions is true:
It is divisible by 4 (year % 4 == 0) and NOT divisible by 100 (year % 100 != 0).
OR, it is divisible by 400 (year % 400 == 0).
If the condition is true, the program prints:
It's a leap year!
If the Year is NOT a Leap Year
else:
print("It's not a leap year.")
If neither of the conditions is met, the year is NOT a leap year.
The program prints:
It's not a leap year.
0 Comments:
Post a Comment