Leap Year check

Definition & Explanation


Let’s create a program to determine if a given year is a leap year or not. The code will incorporate the conditions that define a leap year in order to make this determination. Thus, the following code checks whether a specified year is a leap year or not

Example:
Input: 2020
Output:
Leap Year

Note


Leap years are determined based on two specific conditions. To qualify as a leap year, a year must satisfy either of the following criteria:

  1. The year is divisible by 400.
  2. The year is divisible by 4 but not by 100.
C

Method 1 :

#include <stdio.h>

// Leap year program in C
// If the year satisfies either of the conditions, it's considered a leap year -
// 1. The year must be divisible by 400.
// 2. The year must be divisible by 4 but not 100.
int main() {
    // Set the year to 2020 for demonstration
    int year = 2020;

    // Check the conditions for a leap year
    if (year % 400 == 0)
        printf("%d is a Leap Year\n", year); // Divisible by 400, hence a leap year

    else if (year % 4 == 0 && year % 100 != 0)
        printf("%d is a Leap Year\n", year); // Divisible by 4 but not by 100, hence a leap year

    else
        printf("%d is not a Leap Year\n", year); // Doesn't satisfy either condition, not a leap year

    return 0;
}

Output :

2020 is a Leap Year
C++

Method 1 :

#include <iostream>

// Leap year program in C++
// If the year satisfies either of the conditions, it's considered a leap year -
// 1. The year must be divisible by 400.
// 2. The year must be divisible by 4 but not 100.
int main() {
    // Set the year to 2020 for demonstration
    int year = 2020;

    // Check the conditions for a leap year
    if (year % 400 == 0)
        std::cout << year << " is a Leap Year\n"; // Divisible by 400, hence a leap year

    else if (year % 4 == 0 && year % 100 != 0)
        std::cout << year << " is a Leap Year\n"; // Divisible by 4 but not by 100, hence a leap year

    else
        std::cout << year << " is not a Leap Year\n"; // Doesn't satisfy either condition, not a leap year

    return 0;
}

Output :

2020 is a Leap Year
JAVA

Method 1 :

// Leap year program in Java
// If the year satisfies either of the conditions, it's considered a leap year -
// 1. The year must be divisible by 400.
// 2. The year must be divisible by 4 but not 100.
public class LeapYearChecker {
    public static void main(String[] args) {

        // Set the year to 2020 for demonstration
        int year = 2020;

        // Check the conditions for a leap year
        if (year % 400 == 0)
            System.out.println(year + " is a Leap Year"); // Divisible by 400, hence a leap year

        else if (year % 4 == 0 && year % 100 != 0)
            System.out.println(year + " is a Leap Year"); // Divisible by 4 but not by 100, hence a leap year

        else
            System.out.println(year + " is not a Leap Year"); // Doesn't satisfy either condition, not a leap year
    }
}

Output :

2020 is a Leap Year
Python

Method 1 :

# Leap year program in Python
# If the year satisfies either of the conditions, it's considered a leap year -
# 1. The year must be divisible by 400.
# 2. The year must be divisible by 4 but not 100.

# Set the year to 2020 for demonstration
year = 2020

# Check the conditions for a leap year
if year % 400 == 0:
    print(f"{year} is a Leap Year")  # Divisible by 400, hence a leap year

elif year % 4 == 0 and year % 100 != 0:
    print(f"{year} is a Leap Year")  # Divisible by 4 but not by 100, hence a leap year

else:
    print(f"{year} is not a Leap Year")  # Doesn't satisfy either condition, not a leap year

Output :

2020 is a Leap Year