
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Check Leap Year
A leap year contains one additional day that is added to keep the calendar year synchronized with the astronomical year.
A year that is divisible by 4 is known as a leap year. However, years divisible by 100 are not leap years while those divisible by 400 are.
The program that checks if a year is leap year or not is as follows −
Example
#include<iostream> using namespace std; int main() { int year = 2016; if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) cout<<year<<" is a leap year"; else cout<<year<<" is not a leap year"; return 0; }
Output
2016 is a leap year
In the above program, if a year is divisible by 4 and not divisible by 100, then it is a leap year. Also, if a year is divisible by 400, it is a leap year.
This is demonstrated by the following code snippet.
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) cout<<year<<" is a leap year"; else cout<<year<<" is not a leap year";
The program to check if a year is leap year or not can also be written using nested if statements. This is given as follows −
Example
#include <iostream> using namespace std; int main() { int year = 2020; if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) cout << year << " is a leap year"; else cout << year << " is not a leap year"; } else cout << year << " is a leap year"; } else cout << year << " is not a leap year"; return 0; }
Output
2020 is a leap year
In the above program, if the year is divisible by 4, then it is checked if it is divisible by 100. If it is divisible by 100, then it is checked if it is divisible by 400. It yes, then the year is a leap year.Otherwise, it is not. If the year is not divisible by 100, it is a leap year. If the year is not divisible by 4, it is not a leap year.
This is demonstrated by the following code snippet −
if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) cout << year << " is a leap year"; else cout << year << " is not a leap year"; } else cout << year << " is a leap year"; } else cout << year << " is not a leap year";