Jump to content

Leap year: Difference between revisions

C: optimize, extend tests
(C: optimize, extend tests)
Line 1,351:
<syntaxhighlight lang="c">#include <stdio.h>
 
int is_leap_year(intunsigned year)
{
return (!(year % 4) && (year % 100 ||? !(year3 %: 40015)) ? 1 : 0;
}
 
int main(void)
{
intconst unsigned test_case[] = {1900, 1994, 1996, 1997, 2000}, key, end, year;
1900, 1994, 1996, 1997, 2000, 2024, 2025, 2026, 2100
for (key = 0, end = sizeof(test_case)/sizeof(test_case[0]); key < end; ++key) {
};
year = test_case[key];
forconst (keyunsigned = 0, endn = sizeof( test_case) / sizeof( test_case[0]); key < end; ++key) {
printf("%d is %sa leap year.\n", year, (is_leap_year(year) == 1 ? "" : "not "));
 
for (unsigned i = 0; i != n; ++i) {
unsigned year = test_case[keyi];
printf("%du is %sa leap year.\n", year, (is_leap_year(year) == 1 ? "" : "not "));
}
return 0;
}</syntaxhighlight>
{{out}}
Line 1,371 ⟶ 1,376:
1997 is not a leap year.
2000 is a leap year.
2024 is a leap year.
2025 is not a leap year.
2026 is not a leap year.
2100 is not a leap year.
</pre>
 
559

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.