Python Program: Leap Year
A year is a leap year if it is exactly divisible by 4 but not by 100, with the exception that all centurial years divisible by 400 are leap years.
The year 1900 is divisible by 4 and is also divisible by 100. It is also not divisible by 400. Therefore, it is not a leap year. The year 2000 is divisible by 4 and is also divisible by 100. It is however evenly divisible by 400. Therefore it is a leap year.
The following is a list of some leap years: 1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028. Each of them is either divisible by 4 and not by 100 or evenly divisible by 400.
To implement the above mentioned conditions to check for a leap year, we make use of the modulo operator %
along with the and
and or
operators.
Here is the Python program which tests if a given year is a leap year or not.
year = int(input("Year = "))
if((year % 4 == 0 and year % 100 != 0) or year % 400 == 0):
print(year, " is a leap year")
else:
print(year, " is not a leap year")
On running the above program, a prompt to input some value for Year
appears.
Year =
On entering the year, the program determines whether it is a leap year or not.