首先介绍一下闰年的定义
闰年:能被四或者四百整除但是不能被100整除的年份为闰年
原因:每四年都会多出一天多的时间,所以一般是每四年一个闰年,但是每400年会又多出三天的时间,然后就规定整百(不包括被400整除)的年份不为闰年,这样就可以避免多出来的三天的时间。在百度百科中有详细的时间计算,有兴趣的同学可以去研究一下
判断是否为闰年的代码如下:
#include <stdio.h>
#include <time.h>
//判断是否为闰年
int isLeapyear(int year)
{
if((year%400==0) || (year%100!=0) && (year%4==0))
{
return 1;
}
return 0;
}
void main()
{
int year;
printf("请输入年份:");
scanf("%d",&year);
//is leapyear
if(isLeapyear(year)==1)
{
printf("%d is leapyear.\n",year);
}
else
{
printf("%d is not leapyear.\n",year);
}
}