Leap Year Check in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

  

C Program

#include<stdio.h>

int main() {
    int y;
    scanf("%d", &y);
    printf((y % 4 == 0 && y % 100 != 0) || y % 400 == 0 ? "Leap Year" : "Not Leap Year");
}

C Output

Input: 2024  
Output: Leap Year


C++ Program

#include<iostream>
using namespace std;

int main() {
    int y;
    cin >> y;
    cout << ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0 ? "Leap Year" : "Not Leap Year");
}

C++ Output

Input: 1900  
Output: Not Leap Year


JAVA Program

import java.util.*;

class L {
    public static void main(String[] a) {
        int y = new Scanner(System.in).nextInt();
        System.out.print((y % 4 == 0 && y % 100 != 0) || y % 400 == 0 ? "Leap Year" : "Not Leap Year");
    }
}

JAVA Output

Input: 2000  
Output: Leap Year


Python Program

y = int(input())
print("Leap Year" if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0 else "Not Leap Year")

Python Output

Input: 2023  
Output: Not Leap Year


In-Depth Learning – Whole Concept in Paragraphs
What Is a Leap Year?
A leap year is a year with 366 days rather than the standard 365. Adding this extra day (February 29) assists in keeping the calendar year synchronized with the Earth's orbit around the sun, taking about 365.242 days.
Without leap years, our calendars would gradually move away from true seasons.

How the Logic Works
In order to check whether a year is a leap year or not, the conditions are:

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

But if divisible by 100, it is not a leap year…

…unless also divisible by 400, in which case it is a leap year.

So:

2000 → divisible by 400 → Leap Year

1900 → divisible by 100 but not 400 → Not Leap Year

2024 → evenly divisible by 4 but not 100 → Leap Year

2023 → not evenly divisible by 4 → Not Leap Year

Example
Let's test year = 2000:

2000 % 4 = 0 ✔️

2000 % 100 = 0 ✔️

2000 % 400 = 0 ✔️ → So it is a Leap Year.

Now test year = 1900:

1900 % 4 = 0 ✔️

1900 % 100 = 0 ✔️

1900 % 400 = 300 ✖️ → Not a Leap Year

Real-Life Analogy
Consider calendars as tuning an instrument. Periodically, the instrument (our calendar) gets a little out of tune (out of sync with Earth's orbit). Each 4 years, we "tune it" by adding an extra day (February 29). But that tuning must be just so, which is why the 100-year and 400-year rules are in place — to maintain the long-term calendar correctly aligned.

Why This Is Asked in Interviews
Leap year logic tests your understanding of:

Logical operators (&&, ||)

Modulo operation (%)

Conditional decision making

It’s a classic entry-level coding question that assesses your ability to handle nested conditions and apply mathematical rules in code. It’s often used in MCQs, coding tests, and interview rounds because it's simple yet has real-world application.

Python-Specific Advantage
Python offers a clean and readable method to write the leap year logic in one line with if-else statements. This allows new programmers to learn conditional logic nicely and sets them up for writing efficient solutions.

Search Engine Optimization Optimized Natural Paragraph for Ranking
Looking to easily determine if a specified year is a leap year? This tutorial describes the rule for a leap year using clean, efficient code in C, C++, Java, and Python. A leap year is a crucial idea that ensures our calendar remains synchronized with Earth's orbit. Whether you're creating a calendar application, working on simple logic problems, or practicing for coding interviews, knowing how to implement a leap year checker makes your skills with modulo, conditions, and logical operations more effective. With examples, practical insights, and beautiful code in several languages, this is the ideal guide for anyone who wants to learn leap year logic in programming.