Check If a Year Is a Leap Year or Not in C / C++ / Java / Python / C#

Leap Year

A leap year is a calendar year with 366 days as it contains an extra day in February. Generally, it is considered a leap year when a year is divisible by 4. But if the same year is also divisible by 100 but not 400, it is not viewed as a leap year. For example, 1600, 1936, 2000, 2004, and 2008 are considered leap years, whereas 1700, 1800, 1935, 1953, 1999 are not considered leap years.






1. Algorithm to check if a year is a leap year or not


1. Take a year y as input.

2. Check if y is divisible by 400

3. If step 2 is true, print y as a leap year

4. If step 2 is false, Check if y is divisible by 100

5. If step 4 is true, print y is not a leap year

6. If step 4 is false, Check if y is divisible by 4

7. If step 6 is true, print y is a leap year else print y is not a leap year




2. Pseudocode to check if a year is a leap year or not


Input : A year $y$

Output : $y$ is a leap year or not

1. Procedure leapYear($y$):

2. If $y$ mod 400 == 0:

3. Print $y$ is a leap year

4. Else:

5. If $y$ mod 100 == 0:

6. Print $y$ is not a leap year

7. Else If $y$ mod 4 == 0:

8. Print $y$ is a leap year

9. Else:

10. Print $y$ is not a leap year

11. End Procedure





3. Time complexity to check if a year is a leap year or not


Time Complexity: O(1)





4. Program & output to check if a year is a leap year or not

In this section, the C, C++, Java, Python and C# programs to check if a given year is a leap year or not have been provided. The implementation of the programs is done by using the nested If-Else statements.




4.1. C Program & output to check if a year is a leap year or not

Code has been copied
/*************************************************
        	alphabetacoder.com
C program to check if a year is a leap year or not
**************************************************/
#include <stdio.h>

int main() {
    // declare variables
    int year;

    // take input year 
    printf("Enter the year = ");
    scanf("%d", & year);

    // check if the year is leap year or not
    if (year % 400 == 0)
        printf("%d is a leap year!", year);
    else {
        if (year % 100 == 0)
            printf("%d is not a leap year!", year);
        else if (year % 4 == 0)
            printf("%d is a leap year!", year);
        else
            printf("%d is not a leap year!", year);
    }

    return 0;
}

Output


Case 1:

Enter the year = 1900

1900 is not a leap year!


Case 2:

Enter the year = 2000

2000 is a leap year!


Case 3:

Enter the year = 2004

2004 is a leap year!




4.2. C++ Program & output to check if a year is a leap year or not

Code has been copied
/***************************************************
              alphabetacoder.com
C++ program to check if a year is a leap year or not
****************************************************/
#include <iostream>

using namespace std;

int main() {
    // declare variables
    int year;

    // take input year 
    cout << "Enter the year = ";
    cin >> year;

    // check if the year is leap year or not
    if (year % 400 == 0)
        cout << year << " is a leap year!";
    else {
        if (year % 100 == 0)
            cout << year << " is not a leap year!";
        else if (year % 4 == 0)
            cout << year << " is a leap year!";
        else
            cout << year << " is not a leap year!";
    }

    return 0;
}

Output


Case 1:

Enter the year = 1100

1100 is not a leap year!


Case 2:

Enter the year = 1960

1960 is a leap year!


Case 3:

Enter the year = 2008

2008 is a leap year!




4.3. Java Program & output to check if a year is a leap year or not

Code has been copied
/****************************************************
              alphabetacoder.com
Java program to check if a year is a leap year or not
*****************************************************/

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // declare variables
        int year;

        // create an object of Scanner class
        Scanner sc = new Scanner(System.in);

        // take input of year
        System.out.print("Enter the year = ");
        year = sc.nextInt();

        // check if the year is a leap year or not
        if (year % 400 == 0)
            System.out.print(year + " is a leap year!");
        else {
            if (year % 100 == 0)
                System.out.print(year + " is not a leap year!");
            else if (year % 4 == 0)
                System.out.print(year + " is a leap year!");
            else
                System.out.print(year + " is not a leap year!");
        }
    }
}

Output


Case 1:

Enter the year = 1700

1700 is not a leap year!


Case 2:

Enter the year = 1972

1972 is a leap year!


Case 3:

Enter the year = 2016

2016 is a leap year!




4.4. Python Program & output to check if a year is a leap year or not

Code has been copied
# *******************************************************
#               alphabetacoder.com
# Python program to check if a year is a leap year or not
# *******************************************************

# take input of year
year = int(input("Enter the year = "))

# check if the year is a leap year or not
if year % 400 == 0:
    print(year, "is a leap year!")
else:
    if year % 100 == 0:
        print(year, "is not a leap year!")
    elif year % 4 == 0:
        print(year, "is a leap year!")
    else:
        print(year, "is not a leap year!")

Output


Case 1:

Enter the year = 1500

1500 is not a leap year!


Case 2:

Enter the year = 2020

2020 is a leap year!


Case 3:

Enter the year = 1932

1932 is a leap year!




4.5. C# Program & output to check if a year is a leap year or not

Code has been copied
/**************************************************
        	    alphabetacoder.com
C# program to check if a year is a leap year or not
***************************************************/

using System;

namespace Leapyear
{
    class Program
    {
        static void Main(string[] args)
        {
            // declare variables
            int year;
            
            // take input of year
            Console.Write("Enter the year = ");
            year = Convert.ToInt32(Console.ReadLine());
            
            // check if the year is leap year or not
            if (year % 400 == 0)
                Console.WriteLine(year + " is a leap year!");
            else {
                if (year % 100 == 0)
                    Console.WriteLine(year + " is not a leap year!");
                else if (year % 4 == 0)
                    Console.WriteLine(year + " is a leap year!");
                else
                    Console.WriteLine(year + " is not a leap year!");
            }
            
            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Case 1:

Enter the year = 1400

1400 is not a leap year!


Case 2:

Enter the year = 1912

1912 is a leap year!


Case 3:

Enter the year = 1936

1936 is a leap year!