fbpx

C++: Leap Year

Bjarne-stroustrup
 


Determine whether a given year is a leap year in the Gregorian calendar.

#include <iostream>
 
bool is_leap_year(int year) {
  return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
 
int main() {
  for (auto year : {1900, 1994, 1996, 1997, 2000}) {
    std::cout << year << (is_leap_year(year) ? " is" : " is not") << " a leap year.\n";
  }
}
Output:
1900 is not a leap year.
1994 is not a leap year.
1996 is a leap year.
1997 is not a leap year.
2000 is a leap year.

SOURCE

Content is available under GNU Free Documentation License 1.2.