#include <stdio.h>
#include <assert.h>
int isleap(int year)
{
assert(year > 0 && "isleap: Year must be positive");
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
int main(void)
{
int isleap(int); // declare the function, naming the types of the arguments
printf("%s\n", isleap(2002) ? "Leap year" : "Not leap year");
}
Your algorithm for leap years is wrong. I have modified it. A year is leap if at least one of these conditions is true:
- The year is divisible by four but not by 100.
- The year is divisible by 400.
On a side note, it is better to separate the algorithm and the display of its results in two different functions. isleap
just tells us if a given year is leap. main
relies upon isleap
to report to it this, then prints an appropriate message. This makes our programs easier to read (by humans) and more extensible.