A leap year is a year that has 366 days. An additional day is added to the leap year to keep the calendar year synchronized with the astronomical year. You can check if a given year is a leap year or not using mathematics and programming.
In this article, you’ll learn how to check whether the given year is a leap year or not using C++, Python, JavaScript, and C.
Problem Statement
You’re given a year. You need to check whether the given year is a leap year or not.
Example 1: Let year = 2021.
2021 is not a leap year.
Thus, the output is “2021 is not a leap year”.
Example 2: Let year = 1980.
1980 is a leap year.
Thus, the output is “1980 is a leap year”.
Condition for a Year to Be a Leap Year
A year is a leap year if any or both of the following conditions are satisfied:
- The year is a multiple of 400.
- The year is a multiple of 4 and not a multiple of 100.
C++ Program to Check Whether a Year Is a Leap Year or Not
Below is the C++ program to check whether a given year is a leap year or not:
// C++ program to check if a given year is a leap year or not
#include <iostream>
using namespace std;
bool isLeapYear(int y)
{
// If a year is a multiple of 4 and not a multiple of 100 or a multiple of 400,
// then the year is a leap year
// Multiple of 4 and (Not a multiple of 100 or Multiple of 400) => A Leap year
return (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0));
}
int main()
{
int year1 = 2021;
cout << "Year: " << year1 << endl;
if(isLeapYear(year1))
{
cout << year1 << " is a leap year" << endl;
}
else
{
cout << year1 << " is not a leap year" << endl;
}
int year2 = 2012;
cout << "Year: " << year2 << endl;
if(isLeapYear(year2))
{
cout << year2 << " is a leap year" << endl;
}
else
{
cout << year2 << " is not a leap year" << endl;
}
int year3 = 1980;
cout << "Year: " << year3 << endl;
if(isLeapYear(year3))
{
cout << year3 << " is a leap year" << endl;
}
else
{
cout << year3 << " is not a leap year" << endl;
}
int year4 = 1990;
cout << "Year: " << year4 << endl;
if(isLeapYear(year4))
{
cout << year4 << " is a leap year" << endl;
}
else
{
cout << year4 << " is not a leap year" << endl;
}
int year5 = 1600;
cout << "Year: " << year5 << endl;
if(isLeapYear(year5))
{
cout << year5 << " is a leap year" << endl;
}
else
{
cout << year5 << " is not a leap year" << endl;
}
return 0;
}
Output:
Year: 2021
2021 is not a leap year
Year: 2012
2012 is a leap year
Year: 1980
1980 is a leap year
Year: 1990
1990 is not a leap year
Year: 1600
1600 is a leap year
Python Program to Check Whether a Year Is a Leap Year or Not
Below is the Python program to check whether a given year is a leap year or not:
# Python program to check if a given year is a leap year or not
def isLeapYear(y):
# If a year is a multiple of 4 and not a multiple of 100 or a multiple of 400,
# then the year is a leap year
# Multiple of 4 and (Not a multiple of 100 or Multiple of 400) => A Leap year
return (((y % 4 == 0) and (y % 100 != 0)) or (y % 400 == 0))
year1 = 2021
print("Year: ", year1)
if isLeapYear(year1):
print(year1, "is a leap year")
else:
print(year1, "is not a leap year")
year2 = 2012
print("Year: ", year2)
if isLeapYear(year2):
print(year2, "is a leap year")
else:
print(year2, "is not a leap year")
year3 = 1980
print("Year: ", year3)
if isLeapYear(year3):
print(year3, "is a leap year")
else:
print(year3, "is not a leap year")
year4 = 1990
print("Year: ", year4)
if isLeapYear(year4):
print(year4, "is a leap year")
else:
print(year4, "is not a leap year")
year5 = 1600
print("Year: ", year5)
if isLeapYear(year5):
print(year5, "is a leap year")
else:
print(year5, "is not a leap year")
JavaScript Program to Check Whether a Year Is a Leap Year or Not
Below is the JavaScript program to check whether a given year is a leap year or not:
// JavaScript program to check if a given year is a leap year or not
function isLeapYear(y) {
// If a year is a multiple of 4 and not a multiple of 100 or a multiple of 400,
// then the year is a leap year
// Multiple of 4 and (Not a multiple of 100 or Multiple of 400) => A Leap year
return (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0));
}
var year1 = 2021;
document.write("Year: " + year1 + "<br>");
if(isLeapYear(year1)) {
document.write(year1 + " is a leap year" + "<br>");
} else {
document.write(year1 + " is not a leap year" + "<br>");
}
var year2 = 2012;
document.write("Year: " + year2 + "<br>");
if(isLeapYear(year2)) {
document.write(year2 + " is a leap year" + "<br>");
} else {
document.write(year2 + " is not a leap year" + "<br>");
}
var year3 = 1980;
document.write("Year: " + year3 + "<br>");
if(isLeapYear(year3)) {
document.write(year3 + " is a leap year" + "<br>");
} else {
document.write(year3 + " is not a leap year" + "<br>");
}
var year4 = 1990;
document.write("Year: " + year4 + "<br>");
if(isLeapYear(year4)) {
document.write(year4 + " is a leap year" + "<br>");
} else {
document.write(year4 + " is not a leap year" + "<br>");
}
var year5 = 1600;
document.write("Year: " + year5 + "<br>");
if(isLeapYear(year5)) {
document.write(year5 + " is a leap year" + "<br>");
} else {
document.write(year5 + " is not a leap year" + "<br>");
}
Output:
Year: 2021
2021 is not a leap year
Year: 2012
2012 is a leap year
Year: 1980
1980 is a leap year
Year: 1990
1990 is not a leap year
Year: 1600
1600 is a leap year
C Program to Check Whether a Year Is a Leap Year or Not
Below is the C program to check whether a given year is a leap year or not:
// C program to check if a given year is a leap year or not
#include <stdio.h>
#include <stdbool.h>
bool isLeapYear(int y)
{
// If a year is a multiple of 4 and not a multiple of 100 or a multiple of 400,
// then the year is a leap year
// Multiple of 4 and (Not a multiple of 100 or Multiple of 400) => A Leap year
return (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0));
}
int main()
{
int year1 = 2021;
printf("Year: %d \n", year1);
if(isLeapYear(year1))
{
printf("%d is a leap year \n", year1);
}
else
{
printf("%d is not a leap year \n", year1);
}
int year2 = 2012;
printf("Year: %d \n", year2);
if(isLeapYear(year2))
{
printf("%d is a leap year \n", year2);
}
else
{
printf("%d is not a leap year \n", year2);
}
int year3 = 1980;
printf("Year: %d \n", year3);
if(isLeapYear(year3))
{
printf("%d is a leap year \n", year3);
}
else
{
printf("%d is not a leap year \n", year3);
}
int year4 = 1990;
printf("Year: %d \n", year4);
if(isLeapYear(year4))
{
printf("%d is a leap year \n", year4);
}
else
{
printf("%d is not a leap year \n", year4);
}
int year5 = 1600;
printf("Year: %d \n", year5);
if(isLeapYear(year5))
{
printf("%d is a leap year \n", year5);
}
else
{
printf("%d is not a leap year \n", year5);
}
return 0;
}
Output:
Year: 2021
2021 is not a leap year
Year: 2012
2012 is a leap year
Year: 1980
1980 is a leap year
Year: 1990
1990 is not a leap year
Year: 1600
1600 is a leap year
Make Programming Easier Using Coding Apps
If you’re looking out for a fun way to learn to code, you can use some apps like Enki, Grasshopper, SoloLearn, Codeacademy Go, Hopscotch, Encode, etc. These apps provide great environments for learning to code in a fun and interactive way. They’ll help you stay on top of your game by coding wherever you are.