Skip to content

Leap

Intro

1
2
3
on every year that is evenly divisible by 4
  except every year that is evenly divisible by 100
    unless the year is also evenly divisible by 400

For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap year, but 2000 is.

Task

Given a year, report if it is a leap year.

The Code

1
2
3
4
5
6
7
8
#if !defined(LEAP_H)
#define LEAP_H

namespace leap {
 bool is_leap_year(int test);
}  // namespace leap

#endif // LEAP_H
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include "leap.h"

namespace leap {
bool is_leap_year(int test) {
  if (test % 100 == 0 && (test % 400 != 0))
    return false;
  if (test % 4 == 0)
    return true;

  return false;
}
} // namespace leap

Last update: February 10, 2021

Comments