‹ Return to From Flowcharts to Python Code
Posted on February 28, 2018 by Administrator Posted in
Other challenges you may enjoy...
- Leap Year Subroutine
- From Flowcharts to Python Code
- Weather Statistics Challenge
- Leap Year?
Image Information
- Full Size:
474×499px
A leap year is a calendar year with 366 days as it contains an extra day in February. Generally, it is considered a leap year when a year is divisible by 4. But if the same year is also divisible by 100 but not 400, it is not viewed as a leap year. For example, 1600, 1936, 2000, 2004, and 2008 are considered leap years, whereas 1700, 1800, 1935, 1953, 1999 are not considered leap years. Flowchart to check if a year is a leap year or not has been shown below.
1. Flowchart to Check If a Year Is a Leap Year or Not
There are 365 days in a year. A leap year is a year that has 366 days, instead of 365 days. Hence, a leap year has one day extra and occurs every four years. We can check whether a given year is a leap year or not with the following logic. We usually think that a leap year will be divisible by 4, but apart from this, there are other conditions to do this checking. A year will be a leap year only if:
•It is divisible by 4 and not divisible by 100
•It is divisible by
400
Let us consider an example:
2020/4 = 505.
Therefore, 2020 is a leap year.
2000/100 = 20, 2000/400 = 5 and 2000/4 = 500.
Therefore, 2000 is a leap year.
2022/4 = 505.5
Therefore, 2022 is not a leap year.
Let us have a look at the algorithm and flowchart to check whether a given year is a leap year or not.
Algorithm to check whether a given year is a leap year or not:
Step 1: Start Step 2: Read the value of the year to be checked from the user Step 3: Assign the value to a variable, say ‘year’ Step 4: If (year%4 = 0 AND year%100 != 0) OR year%400 = 0, then: Step 4.1: Display “Leap Year” Step 5: Else Step 5.1: Display “Not Leap Year” Step 6: Stop
Explanation:
We start off by taking the value of the year to be checked as user input and store it in a variable say, ‘year’. To check whether the given year is a leap year or not, we will have to check whether the value is divisible by 4 or not. If it is divisible by 4, we must check whether that value is divisible by 100. It will be a leap year only if it is divisible by 4 and not divisible by 100. If the previous condition is not satisfied, we check whether the value is divisible by 400. If any one of the given conditions is true for the value, the year will be a leap year, otherwise, it will not be a leap year.
We check these conditions for the value with the help of the statement: If (year%4 = 0 AND year%100 != 0) OR year%400 = 0. Here, we use the modulus operator to check for divisibility. If this condition holds true, then we display a message saying that the year is a leap year, else we display that the year is not a leap year.
Note: Here ‘%’ is the modulus operator which returns the remainder value after division.
Example:
Let us consider the year 2000:
year = 2000
2000 % 400 = 0
Therefore, 2000 is a leap year.
Let us consider the year 2000:
year = 2007
year /
4 = 2007 / 4 = 501
2007 % 4 = 3
2007 % 400 = 7
Therefore, 2007 is not a leap year.