Check leap year
A leap year is a year that is divisible by 4, except for years that are divisible by 100 but not by 400. In other words, a leap year has 366 days instead of the usual 365 days, with an extra day added to the month of February (which has 29 days instead of 28).
For example, the year 2020 was a leap year because it is divisible by 4, and the year 1900 was not a leap year because it is divisible by 100 but not by 400. However, the year 2000 was a leap year because it is divisible by both 100 and 400.
The purpose of adding an extra day to the calendar in a leap year is to keep our Gregorian calendar in sync with the solar year, which is approximately 365.2422 days long. Without leap years, our calendar would slowly drift out of sync with the seasons over time.
How to calculate
To calculate if a given year is a leap year or not, you can follow these steps:
Check if the year is divisible by 4. If it is, go to step 2. If it is not, the year is not a leap year.
Check if the year is divisible by 100. If it is, go to step 3. If it is not, the year is a leap year.
Check if the year is divisible by 400. If it is, the year is a leap year. If it is not, the year is not a leap year.
Let's use the year 2024 as an example:
- Is 2024 divisible by 4? Yes.
- Is 2024 divisible by 100? No.
- Is 2024 divisible by 400? No. Therefore, 2024 is a leap year.
Now let's use the year 2100 as an example:
- Is 2100 divisible by 4? Yes.
- Is 2100 divisible by 100? Yes.
- Is 2100 divisible by 400? No. Therefore, 2100 is not a leap year.
Here given code implementation process.
// C Program to
// Check leap year
#include <stdio.h>
//Function which is check whether given year is leap year or not
void is_leap_year(int year)
{
// Check if Year is divisible by 4 or not
// If year is divisible by 4, then check two case
// Case 1 : Check if year is divisible by 100 or not
// Case 2 : Check if Year is divisible by 400 or not
// If any one case true then year is leap year
if (year % 4 == 0 && ((year % 100 != 0) || year % 400 == 0))
{
//When year is leap year
printf("%d is a leap year", year);
}
else
{
printf("%d is not a leap year", year);
}
printf("\n");
}
int main()
{
//Test Case
is_leap_year(2020);
is_leap_year(2018);
is_leap_year(2025);
is_leap_year(2050);
is_leap_year(2032);
is_leap_year(2100);
return 0;
}
Output
2020 is a leap year
2018 is not a leap year
2025 is not a leap year
2050 is not a leap year
2032 is a leap year
2100 is not a leap year
/*
Java Program
Check leap year
*/
class MyMaths
{
//Function which is check whether given year is leap year or not
public void is_leap_year(int year)
{
// Check if Year is divisible by 4 or not
// If year is divisible by 4, then check two case
// Case 1 : Check if year is divisible by 100 or not
// Case 2 : Check if Year is divisible by 400 or not
// If any one case true then year is leap year
if (year % 4 == 0 && ((year % 100 != 0) || year % 400 == 0))
{
//When year is leap year
System.out.print("\n" + year + " is a leap year" );
}
else
{
System.out.print("\n" + year + " is not a leap year");
}
}
public static void main(String[] args)
{
MyMaths obj = new MyMaths();
//Test Case
obj.is_leap_year(2020);
obj.is_leap_year(2018);
obj.is_leap_year(2025);
obj.is_leap_year(2050);
obj.is_leap_year(2032);
obj.is_leap_year(2100);
}
}
Output
2020 is a leap year
2018 is not a leap year
2025 is not a leap year
2050 is not a leap year
2032 is a leap year
2100 is not a leap year
/*
C++ Program
Check leap year
*/
#include<iostream>
using namespace std;
class MyMaths
{
public:
//Function which is check whether given year is leap year or not
void is_leap_year(int year)
{
// Check if Year is divisible by 4 or not
// If year is divisible by 4, then check two case
// Case 1 : Check if year is divisible by 100 or not
// Case 2 : Check if Year is divisible by 400 or not
// If any one case true then year is leap year
if (year % 4 == 0 && ((year % 100 != 0) || year % 400 == 0))
{
cout << "\n" << year << " is a leap year";
}
else
{
cout << "\n" << year << " is not a leap year";
}
}
};
int main()
{
MyMaths obj = MyMaths();
//Test Case
obj.is_leap_year(2020);
obj.is_leap_year(2018);
obj.is_leap_year(2025);
obj.is_leap_year(2050);
obj.is_leap_year(2032);
obj.is_leap_year(2100);
return 0;
}
Output
2020 is a leap year
2018 is not a leap year
2025 is not a leap year
2050 is not a leap year
2032 is a leap year
2100 is not a leap year
<?php
/*
Php Program
Check leap year
*/
class MyMaths
{
//Function which is check whether given year is leap year or not
function is_leap_year($year)
{
// Check if Year is divisible by 4 or not
// If year is divisible by 4, then check two case
// Case 1 : Check if year is divisible by 100 or not
// Case 2 : Check if Year is divisible by 400 or not
// If any one case true then year is leap year
if ($year % 4 == 0 && (($year % 100 != 0) || $year % 400 == 0))
{
echo "\n". $year ." is a leap year";
}
else
{
echo "\n". $year ." is not a leap year";
}
}
}
function main()
{
$obj = new MyMaths();
//Test Case
$obj->is_leap_year(2020);
$obj->is_leap_year(2018);
$obj->is_leap_year(2025);
$obj->is_leap_year(2050);
$obj->is_leap_year(2032);
$obj->is_leap_year(2100);
}
main();
Output
2020 is a leap year
2018 is not a leap year
2025 is not a leap year
2050 is not a leap year
2032 is a leap year
2100 is not a leap year
/*
Node Js Program
Check leap year
*/
class MyMaths
{
//Function which is check whether given year is leap year or not
is_leap_year(year)
{
// Check if Year is divisible by 4 or not
// If year is divisible by 4, then check two case
// Case 1 : Check if year is divisible by 100 or not
// Case 2 : Check if Year is divisible by 400 or not
// If any one case true then year is leap year
if (year % 4 == 0 && ((year % 100 != 0) || year % 400 == 0))
{
process.stdout.write("\n" + year + " is a leap year");
}
else
{
process.stdout.write("\n" + year + " is not a leap year");
}
}
}
function main()
{
var obj = new MyMaths();
//Test Case
obj.is_leap_year(2020);
obj.is_leap_year(2018);
obj.is_leap_year(2025);
obj.is_leap_year(2050);
obj.is_leap_year(2032);
obj.is_leap_year(2100);
}
main();
Output
2020 is a leap year
2018 is not a leap year
2025 is not a leap year
2050 is not a leap year
2032 is a leap year
2100 is not a leap year
/*
C# Program
Check leap year
*/
using System;
class MyMaths
{
//Function which is check whether given year is leap year or not
public void is_leap_year(int year)
{
// Check if Year is divisible by 4 or not
// If year is divisible by 4, then check two case
// Case 1 : Check if year is divisible by 100 or not
// Case 2 : Check if Year is divisible by 400 or not
// If any one case true then year is leap year
if (year % 4 == 0 && ((year % 100 != 0) || year % 400 == 0))
{
Console.Write("\n" + year + " is a leap year");
}
else
{
Console.Write("\n" + year + " is not a leap year");
}
}
public static void Main(String[] args)
{
MyMaths obj = new MyMaths();
//Test Case
obj.is_leap_year(2020);
obj.is_leap_year(2018);
obj.is_leap_year(2025);
obj.is_leap_year(2050);
obj.is_leap_year(2032);
obj.is_leap_year(2100);
}
}
Output
2020 is a leap year
2018 is not a leap year
2025 is not a leap year
2050 is not a leap year
2032 is a leap year
2100 is not a leap year
# Python 3 Program
# Check leap year
class MyMaths :
# Function which is check whether given year is leap year or not
def is_leap_year(self, year) :
# Check if Year is divisible by 4 or not
# If year is divisible by 4, then check two case
# Case 1 : Check if year is divisible by 100 or not
# Case 2 : Check if Year is divisible by 400 or not
# If any one case true then year is leap year
if (year % 4 == 0 and((year % 100 != 0) or year % 400 == 0)) :
print("\n", year ," is a leap year", end = "")
else :
print("\n", year ," is not a leap year", end = "")
def main() :
obj = MyMaths()
# Test Case
obj.is_leap_year(2020)
obj.is_leap_year(2018)
obj.is_leap_year(2025)
obj.is_leap_year(2050)
obj.is_leap_year(2032)
obj.is_leap_year(2100)
if __name__ == "__main__": main()
Output
2020 is a leap year
2018 is not a leap year
2025 is not a leap year
2050 is not a leap year
2032 is a leap year
2100 is not a leap year
# Ruby Program
# Check leap year
class MyMaths
# Function which is check whether given year is leap year or not
def is_leap_year(year)
# Check if Year is divisible by 4 or not
# If year is divisible by 4, then check two case
# Case 1 : Check if year is divisible by 100 or not
# Case 2 : Check if Year is divisible by 400 or not
# If any one case true then year is leap year
if (year % 4 == 0 && ((year % 100 != 0) || year % 400 == 0))
# When year is leap year
print("\n", year ," is a leap year")
else
print("\n", year ," is not a leap year")
end
end
end
def main()
obj = MyMaths.new()
# Test Case
obj.is_leap_year(2020)
obj.is_leap_year(2018)
obj.is_leap_year(2025)
obj.is_leap_year(2050)
obj.is_leap_year(2032)
obj.is_leap_year(2100)
end
main()
Output
2020 is a leap year
2018 is not a leap year
2025 is not a leap year
2050 is not a leap year
2032 is a leap year
2100 is not a leap year
/*
Scala Program
Check leap year
*/
class MyMaths
{
//Function which is check whether given year is leap year or not
def is_leap_year(year: Int): Unit = {
// Check if Year is divisible by 4 or not
// If year is divisible by 4, then check two case
// Case 1 : Check if year is divisible by 100 or not
// Case 2 : Check if Year is divisible by 400 or not
// If any one case true then year is leap year
if (year % 4 == 0 && ((year % 100 != 0) || year % 400 == 0))
{
//When year is leap year
print("\n" + year + " is a leap year");
}
else
{
print("\n" + year + " is not a leap year");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyMaths = new MyMaths();
//Test Case
obj.is_leap_year(2020);
obj.is_leap_year(2018);
obj.is_leap_year(2025);
obj.is_leap_year(2050);
obj.is_leap_year(2032);
obj.is_leap_year(2100);
}
}
Output
2020 is a leap year
2018 is not a leap year
2025 is not a leap year
2050 is not a leap year
2032 is a leap year
2100 is not a leap year
/*
Swift Program
Check leap year
*/
class MyMaths
{
//Function which is check whether given year is leap year or not
func is_leap_year(_ year: Int)
{
// Check if Year is divisible by 4 or not
// If year is divisible by 4, then check two case
// Case 1 : Check if year is divisible by 100 or not
// Case 2 : Check if Year is divisible by 400 or not
// If any one case true then year is leap year
if (year % 4 == 0 && ((year % 100 != 0) || year % 400 == 0))
{
print("\n", year ," is a leap year", terminator: "");
}
else
{
print("\n", year ," is not a leap year", terminator: "");
}
}
}
func main()
{
let obj: MyMaths = MyMaths();
//Test Case
obj.is_leap_year(2020);
obj.is_leap_year(2018);
obj.is_leap_year(2025);
obj.is_leap_year(2050);
obj.is_leap_year(2032);
obj.is_leap_year(2100);
}
main();
Output
2020 is a leap year
2018 is not a leap year
2025 is not a leap year
2050 is not a leap year
2032 is a leap year
2100 is not a leap year
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment