Leap Year Check
Program to check if a year is a leap year
BeginnerTopic: Conditional Programs
C++ Leap Year Check Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter a year: ";
cin >> 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;
}Output
Enter a year: 2024 2024 is a leap year
Understanding Leap Year Check
A leap year is divisible by 4, but not by 100, unless it's also divisible by 400. The condition uses logical operators: (year % 4 == 0 && year % 100 != 0) checks for regular leap years, and (year % 400 == 0) handles century years.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.