Program to check if a given year is leap year

13 Jul, 2022
A year is a leap year if the following conditions are satisfied: 
The year is multiple of 400.The year is multiple of 4 and not multiple of 100.
The year is multiple of 400.
The year is multiple of 4 and not multiple of 100.
Following is pseudo-code 
if year is divisible by 400 then is_leap_year
else if year is divisible by 100 then not_leap_year
else if year is divisible by 4 then is_leap_year
else not_leap_year
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to check if a given// year is leap year or not#include <bits/stdc++.h>using namespace std; bool checkYear(int year){    // If a year is multiple of 400,    // then it is a leap year    if (year % 400 == 0)        return true;     // Else If a year is multiple of 100,    // then it is not a leap year    if (year % 100 == 0)        return false;     // Else If a year is multiple of 4,    // then it is a leap year    if (year % 4 == 0)        return true;    return false;} // Driver codeint main(){    int year = 2000;     checkYear(year) ? cout << "Leap Year":                      cout << "Not a Leap Year";    return 0;} // This is code is contributed// by rathbhupendra
// C program to check if a given// year is leap year or not#include <stdio.h>#include <stdbool.h> bool checkYear(int year){    // If a year is multiple of 400,    // then it is a leap year    if (year % 400 == 0)        return true;     // Else If a year is multiple of 100,    // then it is not a leap year    if (year % 100 == 0)        return false;     // Else If a year is multiple of 4,    // then it is a leap year    if (year % 4 == 0)        return true;    return false;} // driver codeint main(){    int year = 2000;     checkYear(year)? printf("Leap Year"):                   printf("Not a Leap Year");    return 0;}
// Java program to check// for a leap year     class Test{    static boolean checkYear(int year)    {        // If a year is multiple of 400,        // then it is a leap year        if (year % 400 == 0)            return true;             // Else If a year is multiple of 100,        // then it is not a leap year        if (year % 100 == 0)            return false;             // Else If a year is multiple of 4,        // then it is a leap year        if (year % 4 == 0)            return true;        return false;    }             // Driver method    public static void main(String[] args)    {        int year = 2000;        System.out.println( checkYear(2000)? "Leap Year" :                           "Not a Leap Year" );    }}
# Python program to check leap year or notdef checkYear(year):    if (year % 4) == 0:        if (year % 100) == 0:            if (year % 400) == 0:                return True            else:                return False        else:             return True    else:        return False # Driver Codeyear = 2000if(checkYear(year)):    print("Leap Year")else:    print("Not a Leap Year")     # This code is contributed by Chinmoy Lenka
// C# program to check// for a leap yearusing System; class GFG{         static bool checkYear(int year)    {        // If a year is multiple of 400,        // then it is a leap year        if (year % 400 == 0)            return true;             // Else If a year is multiple of 100,        // then it is not a leap year        if (year % 100 == 0)            return false;             // Else If a year is multiple of 4,        // then it is a leap year        if (year % 4 == 0)            return true;        return false;    }             // Driver method    public static void Main()    {        int year = 2000;        Console.Write( checkYear(year)? "Leap Year" :                                 "Not a Leap Year" );    } } // This code is contributed by Sam007
<?php// PHP code to check if a given// year is leap year function checkYear($year){    // If a year is multiple of 400,    // then it is a leap year    if ($year % 400 == 0)        print("Leap Year");             // Else If a year is multiple of 100,    // then it is not a leap year    else if ($year % 100 == 0)        print("Not a Leap Year");                 // Else If a year is multiple of 4,    // then it is a leap year    else if ($year % 4 == 0)        print("Leap Year");             else        print("Not a Leap Year");} // Driver code$year = 2000; checkYear($year);     // This code is contributed by ash264?>
<script> // Javascript program to check// for a leap year     function checkYear( year) {        // If a year is multiple of 400,        // then it is a leap year        if (year % 400 == 0)            return true;         // Else If a year is multiple of 100,        // then it is not a leap year        if (year % 100 == 0)            return false;         // Else If a year is multiple of 4,        // then it is a leap year        if (year % 4 == 0)            return true;        return false;    }     // Driver method              let year = 2000;        document.write(checkYear(2000) ? "Leap Year" : "Not a Leap Year");  // This code is contributed by shikhasingrajput </script>
Leap Year
Time Complexity : O(1)
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
Auxiliary Space: O(1)
How to write the above code in one line? 
C++
C
Java
Python3
C#
PHP
Javascript
// One line C program to check if a// given year is leap year or not#include <bits/stdc++.h>using namespace std; bool checkYear(int year){         // Return true if year is a multiple    // 0f 4 and not multiple of 100.    // OR year is multiple of 400.    return (((year % 4 == 0) && (year % 100 != 0)) ||             (year % 400 == 0));} // Driver codeint main(){    int year = 2000;     checkYear(year)? cout << "Leap Year":                     cout << "Not a Leap Year";    return 0;} // This code is contributed by Akanksha Rai
// One line C program to check if a// given year is leap year or not#include <stdio.h>#include <stdbool.h> bool checkYear(int year){// Return true if year is a multiple// 0f 4 and not multiple of 100.// OR year is multiple of 400.return (((year % 4 == 0) && (year % 100 != 0)) ||        (year % 400 == 0));} // driver codeint main(){    int year = 2000;     checkYear(year)? printf("Leap Year"):                printf("Not a Leap Year");    return 0;}
// Java program to check// for a leap year     class Test{    static boolean checkYear(int year)    {    // Return true if year is a multiple    // of 4 and not multiple of 100.    // OR year is multiple of 400.    return (((year % 4 == 0) && (year % 100 != 0)) ||            (year % 400 == 0));    }             // Driver method    public static void main(String[] args)    {        int year = 2000;        System.out.println(checkYear(2000)? "Leap Year" :                           "Not a Leap Year" );    }}
# Python program to check leap year# or not in a single line def checkYear(year):     # Return true if year is a multiple    # of 4 and not multiple of 100.    # OR year is multiple of 400.    return (((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0)); # Driver Codeyear = 2000if(checkYear(year)):    print("Leap Year")else:    print("Not a Leap Year")     # This code is contributed by Chinmoy Lenka
// C# program to check// for a leap yearusing System; class GFG{    static bool checkYear(int year)    {        // Return true if year is a multiple        // of 4 and not multiple of 100.        // OR year is multiple of 400.        return (((year % 4 == 0) && (year % 100 != 0)) ||                (year % 400 == 0));    }             // Driver method    public static void Main()    {        int year = 2000;        Console.Write( checkYear(year)? "Leap Year" :                                 "Not a Leap Year" );    } }// This code is contributed by Sam007
<?php// PHP code to check if a given// year is leap year function checkYear($year){         // Return true if year is a multiple    // 0f 4 and not multiple of 100.    // OR year is multiple of 400.    return ((($year % 4 == 0) &&             ($year % 100 != 0)) ||             ($year % 400 == 0));} // Driver code$year = 2000; checkYear($year)? print("Leap Year"):                  print("Not a Leap Year"); // This code is contributed by ash264?>
<script>// javascript program to check// for a leap year    function checkYear(year)    {             // Return true if year is a multiple        // of 4 and not multiple of 100.        // OR year is multiple of 400.        return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));    }     // Driver method    var year = 2000;    document.write(checkYear(2000) ? "Leap Year" : "Not a Leap Year");  // This code is contributed by gauravrajput1</script>
Leap Year
Time Complexity: O(1)
Auxiliary Space: O(1)
Check Leap Year using Macros in C/C++
The program outputs 1 if the year is a leap and 0 if it’s not a leap year.
C++
Java
Python3
C#
Javascript
// 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 Codeint main(){    int year = 2020;    cout << ISLP(year) << "\n";    return 0;}
// Java implementation to check// if the year is a leap year// using macrospublic 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 yeardef ISLP(y):  if((y % 400 == 0) or     (y % 100 != 0) and     (y % 4 == 0)):    return 1;  else:    return 0; # Driver codeif __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 macrosusing 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>
1
Time Complexity : O(1)
Auxiliary Space: O(1)
Short Solution in Python : 
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 = 2000if (checkYear(year)):    print("Leap Year")else:    print("Not a Leap Year")        # This code is contributed by Chin
Leap Year
Time Complexity : O(1)
Auxiliary Space: O(1)
Short Solution in Java using isLeap() method of Year class
Year class in java is an in-built class that is used to represent an 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.
Below is the implementation of the above idea:
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
Leap Year
Time Complexity : O(1)
Auxiliary Space: O(1)
ash264
rathbhupendra
Akanksha_Rai
Atul_kumar_Shrivastava
blue_sky
abhishekrajpoot963
divyeshrabadiya07
divyesh072019
pratham76
shikhasingrajput
GauravRajput1
amit143katiyar
shruti456rawal
akashish__
amankr0211
date-time-program
Zoho
School Programming
Zoho
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.