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.
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++ implementation to check // if the year is a leap year // using macros #include <iostream> using namespace std;
// Macro to check if a year // is a leap year #define ISLP(y) ((y % 400 == 0) ||\ (y % 100 != 0) && (y % 4 == 0)) // Driver Code int main()
{ int year = 2020;
cout << ISLP(year) << "\n" ;
return 0;
} |
// Java implementation to check // if the year is a leap year // using macros public class Main
{ // Macro to check if a year
// is a leap year
static int ISLP( int y)
{
if ((y % 400 == 0 ) ||
(y % 100 != 0 ) &&
(y % 4 == 0 ))
{
return 1 ;
}
else
{
return 0 ;
}
}
// Driver code
public static void main(String[] args)
{
int year = 2020 ;
System.out.println(ISLP(year));
}
} // This code is contributed by divyeshrabadiya07. |
# Python3 implementation to check # if the year is a leap year # using macros # Macro to check if a year # is a leap year def ISLP(y):
if ((y % 400 = = 0 ) or
(y % 100 ! = 0 ) and
(y % 4 = = 0 )):
return 1 ;
else :
return 0 ;
# Driver code if __name__ = = '__main__' :
year = 2020 ;
print (ISLP(year));
# This code is contributed by Pratham76.
|
// C# implementation to check // if the year is a leap year // using macros using System;
class GFG {
// Macro to check if a year
// is a leap year
static int ISLP( int y)
{
if ((y % 400 == 0) ||
(y % 100 != 0) &&
(y % 4 == 0))
{
return 1;
}
else
{
return 0;
}
}
// Driver code
static void Main()
{
int year = 2020;
Console.WriteLine(ISLP(year));
}
} // This code is contributed by divyesh072019 |
<script> // javascript implementation to check // if the year is a leap year // using macros // Macro to check if a year
// is a leap year
function ISLP(y)
{
if ((y % 400 == 0) ||
(y % 100 != 0) &&
(y % 4 == 0))
{
return 1;
}
else
{
return 0;
}
}
// Driver code
var year = 2020;
document.write(ISLP(year));
// This code is contributed by Amit Katiyar </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.
#include <iostream> using namespace std;
// Function to check if a given year is a leap year or not bool checkYear( int year) {
// Check if the year is divisible by 4
if (year % 4 == 0) {
// If it's divisible by 100, it should also be divisible by 400 to be a leap year
if (year % 100 == 0) {
return year % 400 == 0;
}
return true ;
}
return false ;
} int main() {
int year = 2000;
// Check if the given year is a leap year or not
if (checkYear(year)) {
cout << "Leap Year" << endl;
} else {
cout << "Not a Leap Year" << endl;
}
// This code is contributed by Shivam Tiwari
return 0;
} |
import java.time.Year;
public class GFG {
// Function to check if a given year is a leap year or not
public static boolean checkYear( int year) {
// Create a Year object representing the given year
Year y = Year.of(year);
// Use the isLeap() method of the Year class to check if the year is a leap year
return y.isLeap();
}
public static void main(String[] args) {
int year = 2000 ;
// Check if the given year is a leap year or not
if (checkYear(year)) {
System.out.println( "Leap Year" );
} else {
System.out.println( "Not a Leap Year" );
}
}
} |
def checkYear(year):
# Return true if year is a multiple
# of 4 and not multiple of 100.
# OR year is multiple of 400.
import calendar
return (calendar.isleap(year))
# Driver Code year = 2000
if (checkYear(year)):
print ( "Leap Year" )
else :
print ( "Not a Leap Year" )
# This code is contributed by Chin |
using System;
public class GFG
{ // Function to check if a given year is a leap year or not
public static bool CheckYear( int year)
{
// Use DateTime.IsLeapYear() method to check if the year is a leap year
return DateTime.IsLeapYear(year);
}
public static void Main( string [] args)
{
int year = 2000;
// Check if the given year is a leap year or not
if (CheckYear(year))
{
Console.WriteLine( "Leap Year" );
}
else
{
Console.WriteLine( "Not a Leap Year" );
}
}
} |
// Function to check if a given year is a leap year or not function checkYear(year) {
// Check if the year is divisible by 4
if (year % 4 === 0) {
// If it's divisible by 100, it should also be divisible by 400 to be a leap year
if (year % 100 === 0) {
return year % 400 === 0;
}
return true ;
}
return false ;
} // Main function function main() {
const year = 2000;
// Check if the given year is a leap year or not
if (checkYear(year)) {
console.log( "Leap Year" );
} else {
console.log( "Not a Leap Year" );
}
// This code is contributed by Shivam Tiwari
} // Run the main function 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.
#include <iostream> using namespace std;
// Function to check if a year is a leap year bool checkYear( int year) {
// Leap year condition:
// 1. If the year is divisible by 4 and not divisible by 100, or
// 2. If the year is divisible by 400.
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true ; // It's a leap year
} else {
return false ; // It's not a leap year
}
} int main() {
int year = 2000; // Sample input to test
if (checkYear(year)) {
cout << "Leap Year" << endl;
} else {
cout << "Not a Leap Year" << endl;
}
return 0;
} |
//Java program to check whether the given year // is a leap year or not import java.io.*;
import java.time.Year;
class GFG {
//returns true if the given year is a leap year.
public static boolean checkYear( int year){
Year y = Year.of(year); //create a Year object of given year
return y.isLeap();
}
//Driver Code
public static void main (String[] args) {
int year = 2000 ; //sample input to test
if (checkYear(year)){ //function call
System.out.println( "Leap Year" );
} else {
System.out.println( "Not a Leap Year" );
}
}
} //This code is contributed by shruti456rawal |
def check_year(year):
# Leap year condition:
# 1. If the year is divisible by 4 and not divisible by 100, or
# 2. If the year is divisible by 400.
if (year % 4 = = 0 and year % 100 ! = 0 ) or (year % 400 = = 0 ):
return True # It's a leap year
else :
return False # It's not a leap year
year = 2000 # Sample input to test
if check_year(year):
print ( "Leap Year" )
else :
print ( "Not a Leap Year" )
|
using System;
class Program {
// Function to check if a year is a leap year
static bool CheckYear( int year) {
// Leap year condition:
// 1. If the year is divisible by 4 and not divisible by 100, or
// 2. If the year is divisible by 400.
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true ; // It's a leap year
} else {
return false ; // It's not a leap year
}
}
static void Main() {
int year = 2000; // Sample input to test
// Call the CheckYear function to determine if it's a leap year
if (CheckYear(year)) {
Console.WriteLine( "Leap Year" );
} else {
Console.WriteLine( "Not a Leap Year" );
}
}
} |
// Function to check if a year is a leap year function checkYear(year) {
// Leap year condition:
// 1. If the year is divisible by 4 and not divisible by 100, or
// 2. If the year is divisible by 400.
if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)) {
return true ; // It's a leap year
} else {
return false ; // It's not a leap year
}
} // Sample input to test var year = 2000;
// Call the checkYear function to determine if it's a leap year 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.