Skip to content

Intro

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

The tricky thing here is that a leap year in the Gregorian calendar occurs:

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

Task

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

The Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include "leap.h"
#include <stdbool.h>

bool leap_year(int year) {

    if (((year % 4) == 0 && (year % 100) != 0) || (year%400)==0)
        return true;

    return false;
}
1
2
3
4
5
6
7
8
#ifndef LEAP_H
#define LEAP_H

#include <stdbool.h>

bool leap_year(int year);

#endif

Last update: February 1, 2021

Comments