Leap year
Turn a rule with exceptions into a logical expression. An easy embedded C problem (Functions), run and graded in the browser.
easy · Functions · bool · logic
A year is a leap year if it is divisible by 4, except that years divisible by 100 are not, unless they are also divisible by 400. So 2024 and 2000 are leap years, and 1900 is not.
Return true if year is a leap year. || means "or", and != means "is not equal to".
Starter code
bool is_leap_year(int year) {
return year % 4 == 0;
}Example tests
Divisible by four
printf("%d\n", is_leap_year(2024));Expected output:
1An ordinary year
printf("%d\n", is_leap_year(2023));Expected output:
0Learn it first
- Functions · Splitting a program into named pieces, passing values in and getting results back.
More Functions problems
- Build on a helper · Write one function and use it inside another.
- Return true or false · Write a function that answers a yes-or-no question.
- Remember a value between calls · Keep state in a variable outside the functions.
- A function with memory · Use a static local variable to count calls.
- Declare before use · Fix code that calls functions the compiler has not seen yet.
- Distance between two values · Reuse a helper to keep the main function simple.