普通闰年:公历年份是4的倍数且不是100的倍数为普通闰年(如2004、2020年就是闰年)。
世纪闰年:公历年份是整百数的,必须是400的倍数才是世界闰年(如2000是世纪闰年,1900不是世纪闰年)。
1.普通方法判断闰年
2.调用函数来判断闰年
//判断是否为闰年
#include <stdio.h>
int is_leap_year(int y)
{
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
return 1;
else
return 0;
}
int main()
{
int year = 0;
int count = 0;
for (year = 1000; year <= 2000; year++)
{
if (is_leap_year(year) == 1)
{
count++;
printf("%d是闰年\n", year);
}
}
printf("%d\n", count);
return 0;
}
巧妙运用函数来求解闰年。