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.
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.
Make sure you have read the "Guides" section of the C track on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.
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.
JavaRanch Cattle Drive, exercise 3 http://www.javaranch.com/leap.jsp
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
#include "vendor/unity.h"
#include "../src/leap.h"
void setUp(void)
{
}
void tearDown(void)
{
}
static void test_a_known_leap_year(void)
{
TEST_ASSERT_TRUE(is_leap_year(1996));
}
static void test_any_old_year(void)
{
TEST_IGNORE(); // delete this line to run test
TEST_ASSERT_FALSE(is_leap_year(1997));
}
static void test_turn_of_the_20th_century(void)
{
TEST_IGNORE();
TEST_ASSERT_FALSE(is_leap_year(1900));
}
static void test_turn_of_the_21st_century(void)
{
TEST_IGNORE();
TEST_ASSERT_TRUE(is_leap_year(2000));
}
static 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);
return UnityEnd();
}
#include "leap.h"
int is_leap_year(int year)
{
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
#ifndef LEAP_H
#define LEAP_H
int is_leap_year(int year);
#endif
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments