In this C++ tutorial, you will learn how to check whether a given year is a leap year using the Gregorian calendar rules. You will also see why years divisible by 100 need a separate check for divisibility by 400.
Leap Year Rules for a C++ Program
A year is a leap year when it satisfies one of these conditions:
- The year is divisible by 400, or
- The year is divisible by 4 but not divisible by 100.
For example, 2024 is a leap year because it is divisible by 4 and not by 100. The year 1900 is not a leap year because it is divisible by 100 but not by 400. The year 2000 is a leap year because it is divisible by 400.
C++ Leap Year Condition
The leap year test can be written as a single Boolean expression. The modulo operator % is used to check whether a year is exactly divisible by 4, 100, or 400.
(year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)
This condition first accepts century years that are divisible by 400. For other years, it accepts years divisible by 4 only when they are not divisible by 100.
Steps to Check a Leap Year in C++
- Read the year as an integer.
- Check whether the year is divisible by 400.
- If not, check whether it is divisible by 4 and not divisible by 100.
- If either leap year condition is true, print that the year is a leap year. Otherwise, print that it is not a leap year.
C++ Program to Check Leap Year Using if…else
#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter year: ";
cin >> year;
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
cout << year << " is a leap year." << endl;
} else {
cout << year << " is not a leap year." << endl;
}
return 0;
}
Example Output for a Leap Year
Enter year: 2024
2024 is a leap year.
Example Output for a Non-Leap Century Year
Enter year: 1900
1900 is not a leap year.
Why Divisibility by 100 and 400 Matters
Checking only year % 4 == 0 is not enough. Century years such as 1700, 1800, and 1900 are divisible by 4, but they are not leap years because they are divisible by 100 and not by 400. Century years such as 1600 and 2000 are leap years because they are divisible by 400.
Leap Year Test Cases for the C++ Condition
| Year | Divisible by 4 | Divisible by 100 | Divisible by 400 | Result |
|---|---|---|---|---|
| 2024 | Yes | No | No | Leap year |
| 2023 | No | No | No | Not a leap year |
| 1900 | Yes | Yes | No | Not a leap year |
| 2000 | Yes | Yes | Yes | Leap year |
Legacy Code Block Retained from the Original Post
The following code block is retained unchanged from the original post content as required. It contains Java code rather than C++ code, so use the C++ example above when compiling this tutorial as C++.
import java.util.Scanner;
/**
* Java Program - Check Leap Year
*/
public class CheckLeapYear {
public static void main(String[] args) {
//read year from user
Scanner scanner = new Scanner(System.in);
System.out.print("Enter year : ");
int year = scanner.nextInt();
//check if year is 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.");
}
scanner.close();
}
}
Output
)
Common Mistakes in C++ Leap Year Programs
- Checking only divisibility by 4: this incorrectly marks 1900 as a leap year.
- Using
&&between all three checks: a normal leap year such as 2024 is not divisible by 400, so that logic would fail. - Reversing the century condition: the correct test requires
year % 100 != 0for years handled by the divisibility-by-4 branch. - Using assignment instead of comparison: use
==when comparing a remainder with zero.
Editorial QA Checklist for the C++ Leap Year Example
- Verify that 2024 is reported as a leap year.
- Verify that 2023 is reported as not a leap year.
- Verify that 1900 is reported as not a leap year.
- Verify that 2000 is reported as a leap year.
- Confirm that the C++ example uses
||between the two valid leap year cases. - Confirm that the non-century branch checks
year % 100 != 0.
C++ Leap Year Check: Key Rule
To check a leap year in C++ Tutorial, test whether the year is divisible by 400, or whether it is divisible by 4 but not by 100. This handles both ordinary leap years and century-year exceptions correctly.
TutorialKart.com