- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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";
- Related Articles
- Java Program to Check Leap Year
- Program to check if a given year is leap year in C
- JavaScript program to check if a given year is leap year
- PHP program to check if a year is leap year or not
- Golang Program to Check Whether a Given Year is a Leap Year
- How to Check Leap Year using Python?
- Check if a given year is leap year in PL/SQL
- Python Pandas - Check whether the year is a leap year from the Period object
- How to find a leap year using C language?
- How to check whether a year or a vector of years is leap year or not in R?
- Java program to find if the given number is a leap year?
- How to check Leap Year in Java 8 How to get current timestamp in Java 8?
- How to find leap year or not in android using year API class?
- How to detect if a given year is a Leap year in Oracle?
- Finding nth day of the year in leap and non-leap years in JavaScript
