Leap Year Program in C, C++ and Java
Leap year logic in C, C++, and Java with three implementation styles: basic conditional, function-based, and stdlib. Code, edge cases, and placement context.
A year is a leap year if it satisfies a 3-condition rule, and writing the check correctly in C, C++, or Java is a near-certain question in freshers’ placement coding rounds.
The rule is simple enough to memorise. The implementation is simple enough to write under interview pressure. The edge cases are where candidates drop marks: 1900 and 2000 both divide evenly by 4, yet they reach opposite answers.
Why a Year Has 366 Days
The Earth takes approximately 365.2425 days to orbit the Sun, not exactly 365. If the calendar never corrects for that 0.2425 fraction, the calendar drifts from the seasons by one full day roughly every four years, and by about three days every 400 years.
The Gregorian calendar correction is:
- Add one day (February 29) every 4 years.
- Subtract it back every 100 years (century years are not leap years).
- Add it back again every 400 years (multiples of 400 are leap years).
The four canonical examples that test every branch of this rule:
| Year | Div by 4? | Div by 100? | Div by 400? | Leap year? |
|---|---|---|---|---|
| 2024 | Yes | No | No | Yes |
| 2023 | No | No | No | No |
| 1900 | Yes | Yes | No | No |
| 2000 | Yes | Yes | Yes | Yes |
Memorise this table. At least one MCQ in most placement aptitude rounds picks from exactly these four years.
The Leap Year Logic in One Expression
The 3-condition rule collapses into a single boolean expression:
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
Reading left to right:
year % 4 == 0— divisible by 4&& year % 100 != 0— but not a century year|| (year % 400 == 0)— unless it’s a multiple of 400
The || short-circuit means: if year % 400 == 0 is true, the entire expression is true regardless of the century check. That handles 2000 correctly.
Tracing through the table:
- 2024:
2024 % 4 = 0and2024 % 100 = 24(not 0) — left clause istrue. Result: leap year. - 1900:
1900 % 4 = 0and1900 % 100 = 0— left clause isfalse.1900 % 400 = 300— right clause isfalse. Result: not a leap year. - 2000:
2000 % 400 = 0— right clause istrue. Result: leap year.
This expression is identical in C, C++, and Java. The language only changes how you read input and print output.
C and C++ Implementation
Basic conditional (C)
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &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;
}
Function-based (C and C++)
Wrapping the check in a named function is the approach interviewers want to see in a coding round. It separates the logic from I/O and makes the function reusable.
// C version
#include <stdio.h>
int isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
printf("%d is %s a leap year.\n", year, isLeapYear(year) ? "" : "not");
return 0;
}
// C++ version
#include <iostream>
using namespace std;
bool isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
cout << year << (isLeapYear(year) ? " is" : " is not") << " a leap year." << endl;
return 0;
}
C++20 standard library
C++20 added std::chrono::year::is_leap(). Per the cppreference documentation, it applies the same Gregorian rule:
#include <chrono>
#include <iostream>
using namespace std;
int main() {
int y;
cin >> y;
bool leap = chrono::year{y}.is_leap();
cout << y << (leap ? " is" : " is not") << " a leap year." << endl;
}
In practice, placement rounds won’t ask for the C++20 version. Know it exists; write the manual expression when asked.
Java Implementation
Basic conditional
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = sc.nextInt();
sc.close();
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.");
}
}
Method-based
import java.util.Scanner;
public class LeapYearMethod {
static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = sc.nextInt();
sc.close();
System.out.println(year + (isLeapYear(year) ? " is" : " is not") + " a leap year.");
}
}
Standard library
Java 8’s java.time package provides Year.isLeap(long prolepticYear). Per the Java SE documentation, it follows the same ISO-8601/Gregorian rule:
import java.time.Year;
public class LeapYearLib {
public static void main(String[] args) {
int year = 2024;
System.out.println(Year.isLeap(year)); // true
}
}
Placement Interview Angle
The leap year check appears in three formats in freshers’ placement rounds.
As MCQ: “Which of the following is not a leap year: 2000, 2004, 2100, 2400?” Correct answer: 2100. This tests the century-exception rule.
As coding: “Write a function that returns true if the input year is a leap year.” Expected: the standard expression wrapped in a function, not an if-else chain with four separate conditions. Interviewers watch for:
- Whether you handle the century exception at all.
- Whether you write a single clean expression or a nested if tree.
- Whether you name the function something self-describing (
isLeapYear, notcheck).
As tracing: “Trace the output of this code for inputs 1900 and 2000.” Given a code snippet, you’re expected to evaluate the expression step by step.
For both DS interview questions and the coding section, see 20 Most Asked Data Structures Interview Questions for the companion pattern library.
The leap year check follows the same pattern as other “check a property of an integer” problems. Like checking whether a string is a palindrome, the discipline is: define the rule precisely, translate it into a single expression, then wrap in a function. The find smallest and largest element in an array article applies the same “define logic, then test edge cases” approach to array traversal.
That discipline (define edge cases first, express the rule precisely, then refactor into a function) is exactly what shows up in AI coding interviews. If you’re moving toward AI-adjacent roles, TinkerLLM lets you apply structured problem-solving to LLM prompts and outputs for ₹299, without committing to a full course.
Primary sources
Frequently asked questions
Why is 1900 not a leap year even though it is divisible by 4?
1900 is divisible by 4 (475 times 4 equals 1900), but it is also divisible by 100. The century exception overrides the 4-year rule unless the year is additionally divisible by 400. 1900 divided by 400 gives 4.75, so it fails that final test and is not a leap year.
What does year % 100 != 0 do in the leap year expression?
It excludes century years from the standard 4-year rule. Without this clause, years like 1900 and 2100 would incorrectly qualify as leap years. The full expression only marks a century year as a leap year if it also passes the year % 400 == 0 check.
Does Java have a built-in method to check leap years?
Yes. java.time.Year.isLeap(year) returns a boolean and implements the same Gregorian rule. It is available from Java 8 onward. For placement rounds that test basic coding, the manual expression is usually expected, but citing the stdlib method shows awareness of production-grade code.
Is the leap year logic the same in Python?
Yes. Python's calendar.isleap(year) applies the identical Gregorian rule. Writing it manually in Python: (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0). The logic is language-agnostic.
What happens if the input year is 0 or negative?
Mathematically, the modulo expression still evaluates. Year 0 is divisible by 400, so it returns true. Negative years work arithmetically in most implementations. Real placement questions constrain input to positive integers, so boundary handling is rarely tested.
How does the leap year question appear in placement tests?
Most often as a multiple-choice question testing which years are and are not leap years (1900, 2000, 2100 are favourite choices), or as a short coding problem where you write the function and trace through 2 to 3 test cases.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)