Trending September 2023 # Leap Year Program In Python # Suggested October 2023 # Top 14 Popular | Chivangcangda.com

# Trending September 2023 # Leap Year Program In Python # Suggested October 2023 # Top 14 Popular

You are reading the article Leap Year Program In Python updated in September 2023 on the website Chivangcangda.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Leap Year Program In Python

Introduction to Leap Year Program in Python

A year is considered a leap year if that year is exactly divisible by 4, except for years that end with 00(century year). A century year is a leap year if that is exactly divisible by 400. However, a year not divisible by 400 and divisible by 100 is not a leap year.

Example:

2004 is a leap year.

2023 is a leap year.

1900 is not a leap year.

2013 is not a leap year.

Examples of Leap Year in Python

Below are the different examples of leap year in Python:

Example #1 – Using Elif

Code:

input_year = int(input("Enter the Year to be checked: ")) if (input_year%400 == 0): print("%d is a Leap Year" %input_year) elif (input_year%100 == 0): print("%d is Not the Leap Year" %input_year) elif (input_year%4 == 0): print("%d is a Leap Year" %input_year) else: print("%d is Not the Leap Year" %input_year)

Output:

Explanation:

First, the user will enter the year of his choice, which needs to be checked for leap year. As per the algorithm, if the year is divisible by 400, that year is a straightway leap year. And so the first if statement is “(input_year%400 == 0)”.

When the year is not divisible by 400, the second If statement will be executed: “(input_year%100 == 0)”. If “(input_year%100 == 0)” evaluates to True. That means it’s a century year but not a leap year.

If both statements don’t evaluate to True, we evaluate the third statement as “input_year%4 == 0”. And if this satisfies, then that year is a leap year, Else not a leap year.

Example #2 – Using Nested If

Code:

input_year = int(input("Enter the Year to be checked: ")) if(input_year%4 == 0): if(input_year%100 == 0): if(input_year%400 == 0): print("%d is Leap Year" %input_year) else: print("%d is Not the Leap Year" %input_year) else: print("%d is Leap Year" %input_year) else: print("%d is Not the Leap Year" %input_year)

Output:

Explanation:

One needs to enter the year he/she wants to check for leap year. The first condition, “(input_year%4 == 0),” will be checked. It will be inside the nested if-else statement if it comes true. Else If condition “(input_year%4 == 0)” evaluates to False, that year is not a leap year.

Now, we evaluate “(input_year%4 == 0)”. We check the condition “(input_year%4 == 0)” for a century year. When condition “(input_year%100 == 0)” evaluates to False, i.e., remainder means that the year is not a century year, and then that is a leap year.

Condition “(input_year%100 == 0)” when evaluates to True, i.e., the remainder is zero. Then it goes the further nested if statement for checking the divisibility with 400. Now “(input_year%400 == 0)” gets evaluated. When the year is divisible by 400, that year is a leap year. Else, not a leap year.

Example #3 – Using Conditional Statement

Code:

input_year = int(input("Enter the Year to be checked: ")) if (( input_year%400 == 0)or (( input_year%4 == 0 ) and ( input_year%100 != 0))): print("%d is Leap Year" %input_year) else: print("%d is Not the Leap Year" %input_year)

Output:

Explanation:

Enter the year, one wishes to check for leap/ non-leap year. Condition (input_year%400 == 0) OR That means any year which is exactly divisible by 400 is undoubtedly a leap year. Condition (( input_year%4 == 0 ) AND ( input_year%100 != 0)). Here it comes from the logic that a year divisible by 4 and not divisible by 100 is a leap year.

First, the condition “( input_year%4 == 0 )” will be evaluated. When it results in True, then condition “( input_year%100 != 0 ) “will be evaluated. Condition “( input_year%100 != 0 ) “estimates whether a year is a century year or not. So, the inference can be made that a Non-century year is a leap year if it’s divisible by 4.

Here are the conditional table of “Or” and “And” to be referred to.

OR:

AND:

Then the result will be printed accordingly.

Example #4 – Using Function

Code:

#function defined def LeapYearCheck(input_year): if (input_year % 4) == 0: if (input_year % 100) == 0: if (input_year % 400) == 0: return True else: return False else: return True else: return False # Driver Code input_year = int(input("Enter the Year to be checked: ")) if(LeapYearCheck(input_year)): print("Leap Year") else: print("Not a Leap Year")

Output:

Explanation:

This approach is similar to approach 2; the steps of approach 2(referred to above) are enclosed as a function. Every time the year needs to be checked for leap/ non-leap year, the function is called.

Writing function for any task is a good Python practice. It helps in the modularity of code and hence enhances the readability of code.

Conclusion

Here we saw the logic of finding leap year and its various ways of implementation in Python. Here we have listed four main approaches; one can also try on many other approaches. There are always many ways of doing the same thing. Here it’s just checking of year, so the speed is not much of a matter. However, if logic is complex, speed does matter a lot.

Recommended Articles

This is a guide to Leap Year Program in Python. Here we discuss the top examples of the Leap Year Program in Python and its code implementation. You can also go through our other related articles to learn more –

You're reading Leap Year Program In Python

Update the detailed information about Leap Year Program In Python on the Chivangcangda.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!