A leap year is a year that is one day longer than a standard year, containing an additional day to keep the calendar year synchronized with the astronomical or seasonal year. This extra day, February 29th, is added every four years to ensure that the calendar year remains close to the length of the solar year.
Problem Statement: Write a Python program to determine if a given year is a leap year or not.
Example:
Input: 2000 Output: 2000 is a Leap Year Input: 2021 Output: 2021 is not a Leap Year
Process to Find Leap Year in Python.
Below are the steps that you can follow to check if the given year is a leap year or not:
- STEP 1: Take a year as input from the user.
- STEP 2: Check if the year is divisible by 4, then move to the next step. If the year is not divisible by 4 then the year is not a Leap Year.
- STEP 3: Check if the year is divisible by 100, then move to the next step. If the year is divisible by 4 but not divisible by 100 then the year is not a Leap Year.
- STEP 4: Check if the year is divisible by 400, then the year is a Leap Year. If the year is divisible by 100 but not divisible by 400 then it is not a Leap Year.
Example:
- The year 2000 was a leap year because it is divisible by 4 and 400.
- The year 1900 was not a leap year because, while divisible by 4, it is also divisible by 100 and not by 400.
Python Program to Find Leap Year.
# Function to check leap year def is_leap_year(year): if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): print(f"{year} is a leap year.") else: print(f"{year} is not a leap year.") # Input year = int(input("Enter a year: ")) # Check and display result is_leap_year(year)
Enter a year: 2000
2000 is a leap year.
Explanation:
In the above Python code, the is_leap_year function takes a year as an argument. It checks if the year is divisible by 4 but not by 100, or if it is divisible by 400. If either condition is true, it prints that the year is a leap year; otherwise, it prints that it's not.
No comments:
Post a Comment