Posted on by Kalkicode
Code Mathematics

Check leap year

In the realm of calendar systems, a leap year is a year that contains an extra day, February 29, to help synchronize the calendar year with the solar year. Leap years play an essential role in maintaining the accuracy of calendars and timekeeping. This article will delve into the concept of leap years and present a C program to determine whether a given year is a leap year or not.

Problem Statement

The problem at hand is to develop a program that can identify whether a given year is a leap year or not. A leap year is usually divisible by 4. However, there are exceptions. Years that are divisible by 100 are not leap years, unless they are also divisible by 400. This accounts for the subtle difference between typical leap years and special leap years.

Example

Consider the year 2000. It is evenly divisible by 4. Normally, this would classify it as a leap year. But, due to the exceptional rule, it is also a century year (divisible by 100). However, it satisfies the final criterion of being divisible by 400. As a result, 2000 is a leap year.

Idea to Solve

The solution lies in creating a function that can determine the leap year status of a given year using the conditions mentioned earlier. Let's outline the steps to achieve this:

Pseudocode

function is_leap_year(year):
    if year is divisible by 4
        if year is not divisible by 100 OR year is divisible by 400
            Print "Leap year"
        else
            Print "Not a leap year"
    else
        Print "Not a leap year"

function main():
    is_leap_year(2020)
    is_leap_year(2018)
    is_leap_year(2025)
    is_leap_year(2050)
    is_leap_year(2032)
    is_leap_year(2100)

Algorithm Explanation

  1. The is_leap_year function takes a single parameter, year.
  2. It checks if the given year is divisible by 4.
  3. If the year is divisible by 4, it further checks two conditions:
    • If the year is not divisible by 100 OR the year is divisible by 400, then it's a leap year.
    • If both conditions are not met, it's not a leap year.
  4. If the year is not divisible by 4, it's not a leap year.
  5. The main function tests the is_leap_year function with various year values.

Code Solution

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

Time Complexity

The time complexity of this code is constant, O(1), as the calculations inside the is_leap_year function involve basic arithmetic operations that don't depend on the input size. The function's execution time remains constant regardless of the input year.

In summary, the provided code successfully determines whether a given year is a leap year or not, taking into account both the general rule and the special rule for centuries. It demonstrates a straightforward implementation of this concept in C.

Comment

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