def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False
year = int(input("Enter a year: "))
if is_leap_year(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
Code Explanation:
Function Definition:
- A function is_leap_year is defined, which takes one argument: year.
Leap Year Logic:
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
else:
return False
- Leap Year Rules:
- A year is a leap year if:
- It is divisible by 4 and not divisible by 100.
- Or, it is divisible by 400.
- A year is a leap year if:
- Explanation of Conditions:
- year % 4 == 0: The year is divisible by 4.
- year % 100 != 0: The year is not divisible by 100 (to exclude years like 1900, 2100 which are divisible by 100 but not leap years).
- year % 400 == 0: The year is divisible by 400 (e.g., 2000, 2400 which are leap years).
- If either condition is true, the function returns True (indicating a leap year), otherwise False.
- Leap Year Rules:
Input:
- year = int(input("Enter a year: "))
- The program prompts the user to input a year, which is converted to an integer and stored in the variable year.
Check Leap Year:
if is_leap_year(year):
print(f"{year} is a leap year.")
print(f"{year} is not a leap year.")
- The function is_leap_year is called with the input year.
- Depending on whether the function returns True or False:
- If True: The year is printed as a leap year.
- If False: The year is printed as not a leap year.
#source code --> clcoding.com
0 Comments:
Post a Comment