ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# vlzware's solution

## to Leap in the C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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

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

``````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.

If your language provides a method in the standard library that does this look-up, pretend it doesn't exist and implement it yourself.

## Notes

Though our exercise adopts some very simple rules, there is more to learn!

For a delightful, four minute explanation of the whole leap year phenomenon, go watch this youtube video.

## Getting Started

Make sure you have read the C page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the `test` task.

``````make test
``````

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

## Source

JavaRanch Cattle Drive, exercise 3 http://www.javaranch.com/leap.jsp

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### test_leap.c

``````#include "vendor/unity.h"
#include "../src/leap.h"

void setUp(void)
{
}

void tearDown(void)
{
}

void test_a_known_leap_year(void)
{
TEST_ASSERT_TRUE(is_leap_year(1996));
}

void test_any_old_year(void)
{
TEST_IGNORE();               // delete this line to run test
TEST_ASSERT_FALSE(is_leap_year(1997));
}

void test_turn_of_the_20th_century(void)
{
TEST_IGNORE();
TEST_ASSERT_FALSE(is_leap_year(1900));
}

void test_turn_of_the_21st_century(void)
{
TEST_IGNORE();
TEST_ASSERT_TRUE(is_leap_year(2000));
}

void test_turn_of_the_25th_century(void)
{
TEST_IGNORE();
TEST_ASSERT_TRUE(is_leap_year(2400));
}

int main(void)
{
UnityBegin("test/test_leap.c");

RUN_TEST(test_a_known_leap_year);
RUN_TEST(test_any_old_year);
RUN_TEST(test_turn_of_the_20th_century);
RUN_TEST(test_turn_of_the_21st_century);
RUN_TEST(test_turn_of_the_25th_century);

UnityEnd();
return 0;
}``````

### src/leap.c

``````#include "leap.h"

int is_leap_year(const int year)
{
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}``````

### src/leap.h

``````#ifndef LEAP_H
#define LEAP_H

int is_leap_year(int year);

#endif``````