Write to code to check whether a given year is leap year or not.

 

Checking for Leap Year

A leap year is a year that is divisible by 4, but if it is divisible by 100, it must also be divisible by 400 to be a leap year. This additional rule corrects for the fact that our calendar year (365 days) is slightly shorter than the actual solar year (365.2422 days).


What is a Leap Year?

A leap year occurs every 4 years to help synchronize the calendar year with the solar year, which is about 365.25 days long. The rules are as follows:

  • A year is a leap year if it is divisible by 4.

  • However, if the year is also divisible by 100, it is not a leap year unless it is also divisible by 400.


Example:

  • 2000 is a leap year (divisible by 4, and by 100, and by 400).

  • 1900 is not a leap year (divisible by 4 and 100, but not by 400).

  • 2024 is a leap year (divisible by 4 and not by 100).


Solutions:

Now let’s see how to check if a given year is a leap year in different programming languages: Java, Python, C, and C++.

1. Java

import java.util.Scanner;

public class LeapYearCheck {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in); // Initialize Scanner for input
        System.out.print("Enter a year: ");
        int year = sc.nextInt(); // Read the input year
        
        // Check if the year is a leap year
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            System.out.println(year + " is a leap year.");
        } else {
            System.out.println(year + " is not a leap year.");
        }

        sc.close(); // Close the Scanner
    }
}

2. Python

# Function to check if a year is a leap year
def is_leap_year(year):
    # Check if the year is divisible by 4 but not 100, or it is divisible by 400
    return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)

# Input from user
year = int(input("Enter a year: "))

# Check and print if the year is a leap year
if is_leap_year(year):
    print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")

3. C++

#include <iostream>
using namespace std;

int main() {
    int year;
    cout << "Enter a year: ";
    cin >> year; // Read the input year

    // Check if the year is a leap year
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
        cout << year << " is a leap year." << endl;
    } else {
        cout << year << " is not a leap year." << endl;
    }

    return 0;
}

4. C

#include <stdio.h>

int main() {
    int year;
    printf("Enter a year: ");
    scanf("%d", &year); // Read the input year

    // Check if the year is a leap year
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
        printf("%d is a leap year.\n", year);
    } else {
        printf("%d is not a leap year.\n", year);
    }

    return 0;
}

Explanation of Code:

Each code version checks if a given year is a leap year based on the following rules:

  • A year is a leap year if it is divisible by 4.

  • However, if the year is also divisible by 100, it must also be divisible by 400 to be a leap year.


Sample Input & Output:

Input: 2000 Output: 2000 is a leap year.

Input: 1900 Output: 1900 is not a leap year.


Conclusion:

Determining whether a year is a leap year is a straightforward problem that helps improve understanding of conditional statements and modulo operations in various programming languages.



**  Please do subscribe my blog for future updates and share with your friends, if you find this informative **

Comments

Popular Posts