A year consisting of 366 days instead of the usual 365 days is a leap year. Every fourth year is a leap year in the Gregorian calendar system. In this article, we will learn how to write a C++ program to check leap year.
A year is a leap year if one of the following conditions is satisfied:
- The year is a multiple of 400.
- The year is a multiple of 4 but not a multiple of 100.
Algorithm to Check Leap Year
The algorithm implements the conditions specified above to check for leap year.
if (year % 400 = 0)
return true (Leap year)
else if (year % 100 = 0)
return false (Not a leap year)
else if (year % 4 = 0)
return true (Leap year)
else
return false (Not a leap year)
endif
Leap Year Program in C++
C++
#include <iostream>
using namespace std;
bool checkYear( int year)
{
if (year % 400 == 0) {
return true ;
}
else if (year % 100 == 0) {
return false ;
}
else if (year % 4 == 0) {
return true ;
}
else {
return false ;
}
}
int main()
{
int year = 2000;
checkYear(year) ? cout << "Leap Year"
: cout << "Not a Leap Year" ;
return 0;
}
|
Complexity Analysis
- Time Complexity: Since there are only if statements in the program, its time complexity is O(1).
- Auxiliary Space: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!