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++
// 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
// 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
# 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#
// 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 |
Javascript
<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.
C++
#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; } |
Java
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" ); } } } |
Python
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 |
C#
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" ); } } } |
Javascript
// 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.
Java
//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 |
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!