Python Program to Check Leap Year or Not

 In this post, we will write a python program to check an inputted program, a leap year or not.




General Idea :

A leap is a year that has 366 days. It will come once in a four year. To check whether a year is a leap year or not can be easily verified with the year itself.

  • If the year is divisible by 400 as the year 2000, it's a leap year.
  • Or otherwise, if the year is not divisible by 100 but divisible by 4 as the year 2004, it's a leap year.
  • Except these everything is not a leap year.
Pre-Requisites :

  • Python Basics
  • Conditional Statements
Program :

n=int(input("Enter a Year "))
if ((n%400==0)or((n%100!=0)and(n%4==0))):
    print(n," is a leap year")
else :
    print(n," is not a leap year")









Explanation :



1. First we need to get the year for which we need to check whether it's a leap year or not. Here we are inputting the year using the input function and storing it to a variable 'n'.




2. Here we are using the if statement to check whether the entered year is a leap year or not. If the either (divisible by 400) or (divisible by 4 but not by 100). We are using the modulus operator (%) which gives the remainder when the inputted year is divided by a number.  We are also using equal to (==) and not equal to (!=) operator.

NOTE : To better understand, please refer to the example output down ( ending of this page).

Note: 

  • If two statements are separated by 'or', then it will be false if both the statements are false, otherwise true.
  • If two statements are separated by 'and, then it will be true if both the statements are true, otherwise false.

We are using two conditions instead of just using divisible by 4 because the centurial years like 1900,1700 are divisible by 4 but not leap years. So for centurial years, we need to check its divisibility with 400 and for non-centurial years, we need to check its divisibility by 4. So we are using these two conditions. 

In the if condition, (n%400==0) will check for centurial years, if it's divisible by 400, then if conditions become true. The second condition for the non-centurial year. To clarify as a non-centurial year, we are checking its divisibility with 100(n%100!=0). If it leaves a remainder while diving, then its non- centurial year. For the non-centurial year, we need to check its divisibility with 4. Hence we are writing (n%4==0).





3. This statement is written under the if statement. That means if the if-condition is true, this statement will be executed. If that condition is true, then the inputted year is a leap year. Hence we are printing that using the print statement.





4. This is an else statement. That means the statements written under it will be executed if the if-condition is false.





5. This is written under the else statement. If the if-statement is false, then the inputted year is not a leap year. Here we are printing that using the print statement.

Input/Output :




Here the inputted year is 2000 which is a centurial year. The program first checks its divisibility using for loop. First, it checks its divisibility with 400. Then the if-statement will be true, hence the print statement under the if-statement will be executed.




                                               



Here in this case the inputted number is 2004 which is a non-centurial year. For that, we need to check its divisibility with 4. First, the program accepts and stores 2004 to n. Then it checks its divisibility with 400. Its remainder is not 0. Now it goes to or-statement. Its checks its divisibility with 100. Its remainder is not 0. Now the first condition in or-statement is true. Now its checks its divisibility with 4, which is also satisfied. Hence the if statements become true.  Then the print under the if-statement is executed.






Now, in this case, n is 2009. It enters if statement. Its checks its divisibility with 400. It fails. Then it enters or-statement. First, it checks its divisibility with 100. It passes. Then the and- statement executes. Its checks its divisibility with 4. It fails. As we used and-operator, it will be true, if both the conditions are true ( i.e,(n%100!=0)and(n%4==0)).Hence the whole the if-condition becomes false. Hence the print statement under else-statement will be executed.






Now, in this case, n is 1900. It enters if-statement. It checks its divisibility with 400. Its leaves remainder when divided by 400. Hence it fails. It enters the or-statement. It checks its divisibility with 100. It doesn't leave the remainder when divided with 100. Hence it fails ( as we used not equal to {!=} operator, here in this case remainder is 0). It's written with and statement. If one of the and-statement is false then the whole and-statement is false. Now both the or-statement is false. Hence the whole if-statement is false. Hence the print statement under else-statement will be executed.

Comments

Popular posts from this blog

Python Program to Print Fibonacci series - Codicaly

Python - Program to find the given number is a prime or composite number

Python Program To Replace The Elements of List With its Cube