How to find the year is leap year or not in python

Python Program to Check Leap Year

Leap Year:

A year is called a leap year if it contains an additional day which makes the number of the days in that year is 366. This additional day is added in February which makes it 29 days long.

A leap year occurred once every 4 years.

How to determine if a year is a leap year?

You should follow the following steps to determine whether a year is a leap year or not.

  1. If a year is evenly divisible by 4 means having no remainder then go to next step. If it is not divisible by 4. It is not a leap year. For example: 1997 is not a leap year.
  2. If a year is divisible by 4, but not by 100. For example: 2012, it is a leap year. If a year is divisible by both 4 and 100, go to next step.
  3. If a year is divisible by 100, but not by 400. For example: 1900, then it is not a leap year. If a year is divisible by both, then it is a leap year. So 2000 is a leap year.

Look at the following program to understand the implementation of it:

Example:

Output:

Enter the number: 1700
Given year is not a leap Year

Explanation:

We have implemented all the three mandatory conditions (that we listed above) in the program by using the 'and' and 'or' keyword inside the if else condition. After getting input from the user, the program first will go to the input part and check if the given year is a leap year. If the condition satisfies, the program will print 'Leap Year'; else program will print 'Not a leap year.'



How to find the year is leap year or not in python
For Videos Join Our Youtube Channel: Join Now


Feedback

  • Send your Feedback to [email protected]

Help Others, Please Share

How to find the year is leap year or not in python
How to find the year is leap year or not in python
How to find the year is leap year or not in python






A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400. For example,

2017 is not a leap year
1900 is a not leap year
2012 is a leap year
2000 is a leap year

Source Code

# Python program to check if year is a leap year or not

year = 2000

# To get year (integer input) from the user
# year = int(input("Enter a year: "))

# divided by 100 means century year (ending with 00)
# century year divided by 400 is leap year
if (year % 400 == 0) and (year % 100 == 0):
    print("{0} is a leap year".format(year))

# not divided by 100 means not a century year
# year divided by 4 is a leap year
elif (year % 4 ==0) and (year % 100 != 0):
    print("{0} is a leap year".format(year))

# if not divided by both 400 (century year) and 4 (not century year)
# year is not leap year
else:
    print("{0} is not a leap year".format(year))

Output

2000 is a leap year

You can change the value of year in the source code and run it again to test this program.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    A year is a leap year if the following conditions are satisfied: 

    1. The year is multiple of 400.
    2. The year is multiple of 4 and not multiple of 100.

    How to find the year is leap year or not in python

    Leap year or not.

    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

    Example 1: Checking all the possible conditions for a leap year.

    C++

    #include

    using namespace std;

    bool checkYear(int year)

    {

        if (year % 400 == 0)

            return true;

        if (year % 100 == 0)

            return false;

        if (year % 4 == 0)

            return true;

        return false;

    }

    int main()

    {

        int year = 2000;

        checkYear(year) ? cout << "Leap Year":

                          cout << "Not a Leap Year";

        return 0;

    }

    C

    #include

    #include

    bool checkYear(int year)

    {

        if (year % 400 == 0)

            return true;

        if (year % 100 == 0)

            return false;

        if (year % 4 == 0)

            return true;

        return false;

    }

    int main()

    {

        int year = 2000;

        checkYear(year)? printf("Leap Year"):

                       printf("Not a Leap Year");

        return 0;

    }

    Java

    class Test

    {

        static boolean checkYear(int year)

        {

            if (year % 400 == 0)

                return true;

            if (year % 100 == 0)

                return false;

            if (year % 4 == 0)

                return true;

            return false;

        }

        public static void main(String[] args)

        {

            int year = 2000;

            System.out.println( checkYear(2000)? "Leap Year" :

                               "Not a Leap Year" );

        }

    }

    Python3

    def 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

    year = 2000

    if(checkYear(year)):

        print("Leap Year")

    else:

        print("Not a Leap Year")

    C#

    using System;

    class GFG

    {

        static bool checkYear(int year)

        {

            if (year % 400 == 0)

                return true;

            if (year % 100 == 0)

                return false;

            if (year % 4 == 0)

                return true;

            return false;

        }

        public static void Main()

        {

            int year = 2000;

            Console.Write( checkYear(year)? "Leap Year" :

                                     "Not a Leap Year" );

        }

    }

    PHP

    function checkYear($year)

    {

        if ($year % 400 == 0)

            print("Leap Year");

        else if ($year % 100 == 0)

            print("Not a Leap Year");

        else if ($year % 4 == 0)

            print("Leap Year");

        else

            print("Not a Leap Year");

    }

    $year = 2000;

    checkYear($year);

    ?>

    Javascript

    Output:

    Leap Year

    Time Complexity : O(1)

    Auxiliary Space: O(1)

    Example 2: One line code for checking whether the year is a leap year or not.

    C++

    #include

    using namespace std;

    bool checkYear(int year)

    {

        return (((year % 4 == 0) && (year % 100 != 0)) ||

                 (year % 400 == 0));

    }

    int main()

    {

        int year = 2000;

        checkYear(year)? cout << "Leap Year":

                         cout << "Not a Leap Year";

        return 0;

    }

    C

    #include

    #include

    bool checkYear(int year)

    {

    return (((year % 4 == 0) && (year % 100 != 0)) ||

            (year % 400 == 0));

    }

    int main()

    {

        int year = 2000;

        checkYear(year)? printf("Leap Year"):

                    printf("Not a Leap Year");

        return 0;

    }

    Java

    class Test

    {

        static boolean checkYear(int year)

        {

        return (((year % 4 == 0) && (year % 100 != 0)) ||

                (year % 400 == 0));

        }

        public static void main(String[] args)

        {

            int year = 2000;

            System.out.println(checkYear(2000)? "Leap Year" :

                               "Not a Leap Year" );

        }

    }

    Python3

    def checkYear(year):

        return (((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0));

    year = 2000

    if(checkYear(year)):

        print("Leap Year")

    else:

        print("Not a Leap Year")

    C#

    using System;

    class GFG

    {

        static bool checkYear(int year)

        {

            return (((year % 4 == 0) && (year % 100 != 0)) ||

                    (year % 400 == 0));

        }

        public static void Main()

        {

            int year = 2000;

            Console.Write( checkYear(year)? "Leap Year" :

                                     "Not a Leap Year" );

        }

    }

    PHP

    function checkYear($year)

    {

        return ((($year % 4 == 0) &&

                 ($year % 100 != 0)) ||

                 ($year % 400 == 0));

    }

    $year = 2000;

    checkYear($year)? print("Leap Year"):

                      print("Not a Leap Year");

    ?>

    Javascript

    Output:

    Leap Year

    Time Complexity: O(1)

    Auxiliary Space: O(1)

    Example 3: Check Leap Year using Macros in C/C++

    C++

    #include

    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

    Output:

    1

    Time Complexity : O(1)

    Auxiliary Space: O(1)

    Explanation:

    The program outputs 1 if the year is a leap and 0 if it’s not a leap year.

    Example 4: Short Solution in Python 

    Python

    def checkYear(year):

        import calendar

        return(calendar.isleap(year))

    year = 2000

    if (checkYear(year)):

        print("Leap Year")

    else:

        print("Not a Leap Year")

    Output:

    Leap Year

    Time Complexity : O(1)

    Auxiliary Space: O(1)

    Example 5: Short Solution in Java using isLeap() method of Year class

    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");

            }

        }

    }

    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 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:


    How do you test a year is leap year or not?

    To check if a year is a leap year, divide the year by 4. If it is fully divisible by 4, it is a leap year. For example, the year 2016 is divisible 4, so it is a leap year, whereas, 2015 is not. However, Century years like 300, 700, 1900, 2000 need to be divided by 400 to check whether they are leap years or not.

    Is 1992 a leap year Python?

    It is however evenly divisible by 400. Therefore it is a leap year. The following is a list of some leap years: 1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028.

    Is 1900 a leap year Python?

    Exceptions: 1900 is not a leap year because it is divisible by 4 and 100, but not by 400. 2000 is a leap year because it is divisible by 4, 100, and 400.

    Is 2100 a leap year Python?

    Since 2100 is a century year, and it should be divisible by 400 to be a Leap Year, but it is not divisible by 400 hence it is not a Leap Year.