Program to check if a given year is leap year
Last Updated :
28 Nov, 2023
A leap year is a year that contains an additional day, February 29th, making it 366 days long instead of the usual 365 days. Leap years are necessary to keep our calendar in alignment with the Earth’s revolutions around the Sun.
A year is a leap year if the following conditions are satisfied:
- The year is multiple of 400.
- The year is a multiple of 4 and not a multiple of 100.
Leap year or not.
Program to Check Leap Year Using Macros
Here we will use macros to check the leap year. In macros, we will use the necessary condition to check the leap year and return the answer.
C++
#include <iostream>
using namespace std;
#define ISLP(y) ((y % 400 == 0) ||\
(y % 100 != 0) && (y % 4 == 0))
int main()
{
int year = 2020;
cout << ISLP(year) << "\n" ;
return 0;
}
|
Java
public class Main
{
static int ISLP( int y)
{
if ((y % 400 == 0 ) ||
(y % 100 != 0 ) &&
(y % 4 == 0 ))
{
return 1 ;
}
else
{
return 0 ;
}
}
public static void main(String[] args)
{
int year = 2020 ;
System.out.println(ISLP(year));
}
}
|
Python3
def ISLP(y):
if ((y % 400 = = 0 ) or
(y % 100 ! = 0 ) and
(y % 4 = = 0 )):
return 1 ;
else :
return 0 ;
if __name__ = = '__main__' :
year = 2020 ;
print (ISLP(year));
|
C#
using System;
class GFG {
static int ISLP( int y)
{
if ((y % 400 == 0) ||
(y % 100 != 0) &&
(y % 4 == 0))
{
return 1;
}
else
{
return 0;
}
}
static void Main()
{
int year = 2020;
Console.WriteLine(ISLP(year));
}
}
|
Javascript
<script>
function ISLP(y)
{
if ((y % 400 == 0) ||
(y % 100 != 0) &&
(y % 4 == 0))
{
return 1;
}
else
{
return 0;
}
}
var year = 2020;
document.write(ISLP(year));
</script>
|
Explanation: The program outputs 1 if the year is a leap and 0 if it’s not a leap year.
Output:
1
Time Complexity : O(1)
Auxiliary Space: O(1)
Short Solution of leap year program
In this example, we have used the built-in isleap() function of the calendar module to check the leap year program in Python.
C++
#include <iostream>
using namespace std;
bool checkYear( int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
return year % 400 == 0;
}
return true ;
}
return false ;
}
int main() {
int year = 2000;
if (checkYear(year)) {
cout << "Leap Year" << endl;
} else {
cout << "Not a Leap Year" << endl;
}
return 0;
}
|
Java
import java.time.Year;
public class GFG {
public static boolean checkYear( int year) {
Year y = Year.of(year);
return y.isLeap();
}
public static void main(String[] args) {
int year = 2000 ;
if (checkYear(year)) {
System.out.println( "Leap Year" );
} else {
System.out.println( "Not a Leap Year" );
}
}
}
|
Python
def checkYear(year):
import calendar
return (calendar.isleap(year))
year = 2000
if (checkYear(year)):
print ( "Leap Year" )
else :
print ( "Not a Leap Year" )
|
C#
using System;
public class GFG
{
public static bool CheckYear( int year)
{
return DateTime.IsLeapYear(year);
}
public static void Main( string [] args)
{
int year = 2000;
if (CheckYear(year))
{
Console.WriteLine( "Leap Year" );
}
else
{
Console.WriteLine( "Not a Leap Year" );
}
}
}
|
Javascript
function checkYear(year) {
if (year % 4 === 0) {
if (year % 100 === 0) {
return year % 400 === 0;
}
return true ;
}
return false ;
}
function main() {
const year = 2000;
if (checkYear(year)) {
console.log( "Leap Year" );
} else {
console.log( "Not a Leap Year" );
}
}
main();
|
Output:
Leap Year
Time Complexity : O(1)
Auxiliary Space: O(1)
Short Solution in Java using isLeap() method of Year class
In this Java program, we will the inbuilt isLeap() method of the Java year class to check the leap Year.
C++
#include <iostream>
using namespace std;
bool checkYear( int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true ;
} else {
return false ;
}
}
int main() {
int year = 2000;
if (checkYear(year)) {
cout << "Leap Year" << endl;
} else {
cout << "Not a Leap Year" << endl;
}
return 0;
}
|
Java
import java.io.*;
import java.time.Year;
class GFG {
public static boolean checkYear( int year){
Year y = Year.of(year);
return y.isLeap();
}
public static void main (String[] args) {
int year = 2000 ;
if (checkYear(year)){
System.out.println( "Leap Year" );
} else {
System.out.println( "Not a Leap Year" );
}
}
}
|
Python3
def check_year(year):
if (year % 4 = = 0 and year % 100 ! = 0 ) or (year % 400 = = 0 ):
return True
else :
return False
year = 2000
if check_year(year):
print ( "Leap Year" )
else :
print ( "Not a Leap Year" )
|
C#
using System;
class Program {
static bool CheckYear( int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true ;
} else {
return false ;
}
}
static void Main() {
int year = 2000;
if (CheckYear(year)) {
Console.WriteLine( "Leap Year" );
} else {
Console.WriteLine( "Not a Leap Year" );
}
}
}
|
Javascript
function checkYear(year) {
if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)) {
return true ;
} else {
return false ;
}
}
var year = 2000;
if (checkYear(year)) {
console.log( "Leap Year" );
} else {
console.log( "Not a Leap Year" );
}
|
Output:
Leap Year
Time Complexity : O(1)
Auxiliary Space: O(1)
Explanation:
Year class in java is an in-built class that is used to represent a year as a date-time immutable object. The idea is to invoke the isLeap() method of the class which returns true if the year is a leap year and vice-versa.
Share your thoughts in the comments
Please Login to comment...