Program to check leap year in Python. This program tells about the leap year concept (scientific concept) calculated by mathematics and its programming done by if else concept. The source code with step by step explanation and output are also covered in this post. This program is asked from beginners, class 11,12 and B Tech computer science students. Download source code.
Program to check leap year in Python
Conceptual Knowledge :
Leap Year:
A year is called a leap year if it contains an additional day which makes the number of the days in that year is 366. This additional day is added in February which makes it 29 days long.
A leap year occurred once every 4 years
We should follow the following steps to determine whether a year is a leap year or not.
- If a year is evenly divisible by 4 means having no remainder then go to next step. If it is not divisible by 4. It is not a leap year. For example: 1997 is not a leap year.
- If a year is divisible by 4, but not by 100. For example: 2012, it is a leap year. If a year is divisible by both 4 and 100, go to next step.
- If a year is divisible by 100, but not by 400. For example: 1900, then it is not a leap year. If a year is divisible by both, then it is a leap year. So 2000 is a leap year.
Source code:
def CheckLeap(Year):
if((Year % 400 == 0) or
(Year % 100 != 0) and
(Year % 4 == 0)):
print("Given Year is a leap Year");
else:
print ("Given Year is not a leap Year")
Year = int(input("Enter the number: "))
CheckLeap(Year)
Download Source code : For Download Click on the below Button
Step wise Source Code Explanation :
Step 1 : Default function to implement conditions to check leap year
def CheckLeap(Year):
Step 2 : Checking if the given year is leap year
if((Year % 400 == 0) or
(Year % 100 != 0) and
(Year % 4 == 0)):
print(“Given Year is a leap Year”);
Step 3 :# Else it is not a leap year
else:
print (“Given Year is not a leap Year”)
Step 4: Taking an input year from user
Year = int(input(“Enter the number: “))
CheckLeap(Year)
Output :
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py Enter the number: 2000 Given Year is a leap Year >>> RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py Enter the number: 1000 Given Year is not a leap Year >>>
Output Explanation :
The year is captured.
Enter the number: 2000
It is calculated what the given year is
Given Year is a leap Year
Next Output after new run command
Enter the number: 1000
Given Year is not a leap Year
Conclusion :
Leap years are calculated correctly by the given program.
Thank you, for reading and learning from this Program to check leap year in Python post. This program tells about the leap year concept (scientific concept) calculated by mathematics and its programming done by if else concept. This program is asked from beginners, class 11,12 and B Tech computer science students. In below link there are more basic beginners programs and also the practical file of class 11 and 12 computer science with python.
Python Programs Archives – CS Study
Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study
Computer Science Practical file for class 12 Term-2 – CS Study