C++ Program to Check Leap Year
A leap year is a calendar year in which an additional day is added to February month. In a leap year, the number of days in February month and the year are 29 and 366 respectively. A year that is not a leap year is called a common year. A year is said to be a leap year if:
- it is divisible by 4.
- it is divisible by 4 but not divisible by 100.
- it is divisible by 4, 100 and 400.
Method 1: Using conditional statements
In the example below, conditional statements are used to identify a leap year.
#include <iostream> using namespace std; int main() { int year = 2019; if (year % 400 == 0) { cout<<year<<" is a leap year."; } else if (year % 100 == 0) { cout<<year<<" is not a leap year."; } else if (year % 4 == 0) { cout<<year<<" is a leap year."; } else { cout<<year<<" is not a leap year."; } return 0; }
The above code will give the following output:
2019 is not a leap year.
Method 2: Using function
In the example below, a function called leapyear() is created which takes year as argument and prints whether the passed year is a leap year or not.
#include <iostream> using namespace std; static void leapyear(int); static void leapyear(int year) { if (year % 400 == 0) { cout<<year<<" is a leap year."; } else if (year % 100 == 0) { cout<<year<<" is not a leap year."; } else if (year % 4 == 0) { cout<<year<<" is a leap year."; } else { cout<<year<<" is not a leap year."; } } int main() { leapyear(2019); return 0; }
The above code will give the following output:
2019 is not a leap year.
Recommended Pages
- C++ - Swap two numbers
- C++ Program - Fibonacci Sequence
- C++ Program - Insertion Sort
- C++ Program - Find Factorial of a Number
- C++ Program - Find HCF of Two Numbers
- C++ Program - Merge Sort
- C++ Program - Shell Sort
- Stack in C++
- Queue in C++
- C++ Program - Find LCM of Two Numbers
- C++ Program - To Check Whether a Number is Palindrome or Not
- C++ Program - To Check Whether a String is Palindrome or Not
- C++ Program - Heap Sort
- C++ Program - Quick Sort
- C++ - Swap Two Numbers without using Temporary Variable
- C++ Program - To Check Armstrong Number
- C++ Program - Counting Sort
- C++ Program - Radix Sort
- C++ Program - Find Largest Number among Three Numbers
- C++ Program - Print Floyd's Triangle