Python Program to Check Leap Year
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.
How to determine if a year is a leap year?
You 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.
Look at the following program to understand the implementation of it:
Example:
Output:
Enter the number: 1700 Given year is not a leap Year
Explanation:
We have implemented all the three mandatory conditions [that we listed above] in the program by using the 'and' and 'or' keyword inside the if else condition. After getting input from the user, the program first will go to the input part and check if the given year is a leap year. If the condition satisfies, the program will print 'Leap Year'; else program will print 'Not a leap year.'
Feedback
- Send your Feedback to [email protected]
Help Others, Please Share
A leap year is exactly divisible by 4 except for century years [years ending with 00]. The century year is a leap year only if it is perfectly divisible by 400. For example,
2017 is not a leap year 1900 is a not leap year 2012 is a leap year 2000 is a leap year
Source Code
# Python program to check if year is a leap year or not
year = 2000
# To get year [integer input] from the user
# year = int[input["Enter a year: "]]
# divided by 100 means century year [ending with 00]
# century year divided by 400 is leap year
if [year % 400 == 0] and [year % 100 == 0]:
print["{0} is a leap year".format[year]]
# not divided by 100 means not a century year
# year divided by 4 is a leap year
elif [year % 4 ==0] and [year % 100 != 0]:
print["{0} is a leap year".format[year]]
# if not divided by both 400 [century year] and 4 [not century year]
# year is not leap year
else:
print["{0} is not a leap year".format[year]]
Output
2000 is a leap year
You can change the value of year in the source code and run it again to test this program.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
A year is a leap year if the following conditions are satisfied:
- The year is multiple of 400.
- The year is multiple of 4 and not multiple of 100.
Leap year or not.
pseudo-code
- if year is divisible by 400 then is_leap_year
- else if year is divisible by 100 then not_leap_year
- else if year is divisible by 4 then is_leap_year
- else not_leap_year
Example 1: Checking all the possible conditions for a leap year.
C++
#include
using
namespace
std;
bool
checkYear[
int
year]
{
if
[year % 400 == 0]
return
true
;
if
[year % 100 == 0]
return
false
;
if
[year % 4 == 0]
return
true
;
return
false
;
}
int
main[]
{
int
year = 2000;
checkYear[year] ? cout
Javascript
function
checkYear[ year] {
if
[year % 400 == 0]
return
true
;
if
[year % 100 == 0]
return
false
;
if
[year % 4 == 0]
return
true
;
return
false
;
}
let year = 2000;
document.write[checkYear[2000] ?
"Leap Year"
:
"Not a Leap Year"
];
Output:
Leap Year
Time Complexity : O[1]
Auxiliary Space: O[1]
Example 2: One line code for checking whether the year is a leap year or not.
C++
#include
using
namespace
std;
bool
checkYear[
int
year]
{
return
[[[year % 4 == 0] && [year % 100 != 0]] ||
[year % 400 == 0]];
}
int
main[]
{
int
year = 2000;
checkYear[year]? cout
Javascript
function
checkYear[year]
{
return
[[[year % 4 == 0] && [year % 100 != 0]] || [year % 400 == 0]];
}
var
year = 2000;
document.write[checkYear[2000] ?
"Leap Year"
:
"Not a Leap Year"
];
Output:
Leap Year
Time Complexity: O[1]
Auxiliary Space: O[1]
Example 3: Check Leap Year using Macros in C/C++
C++
#include
using
namespace
std;
#define ISLP[y] [[y % 400 == 0] ||\
[y % 100 != 0] && [y % 4 == 0]]
int
main[]
{
int
year = 2020;
cout