on-internet
← Community

Is there a simpler test for leap year in C?

Stack Overflow ·

Is there any shorter logic than this to achieve leap year test with fewer conditions? #include <stdio.h> int isleap(int year) { return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); } int main() { for (int year = 1600; year <= 2020; year++) { printf("%d: %s\n", year, isleap(year) ? "Leap Year" : "Not a Leap Year"); } return 0; }

Comments

Sign in or sign up to comment and upvote.

No comments yet.