//闰年判断条件,满足以下任意一个条件即可
//年份是4的倍数并且不是100的倍数
//年份是400的倍数
#include <stdio.h>
int main()
{
int year = 0;
int flag = 0;
printf("请输入要判断的年份:");
scanf("%d",&year);
if(year%4 == 0)
{
if(year%100!=0)
flag = 1;
}
if(year%400 == 0)
flag = 1;
if(flag)
printf("%d是闰年\n",year);
else
printf("%d不是闰年\n",year);
return 0;
}
用if语句简化形式:
#include <stdio.h>
int main()
{
int year = 0;
int flag = 0;
printf("请输入要判断的年份:");
scanf("%d",&year);
if((year%4 == 0) && (year%100!=0)||(year%400 == 0))
printf("%d年是闰年\n",year);
else
printf("%d年不是闰年\n",year);
return 0;
}
函数形式:
#include <stdio.h>
//是闰年返回1
//不是闰年返回0
int Is_leapyear(int year)
{
if((year%4 == 0) && (year%100!=0)||(year%400 == 0))
return 1;
else
return 0;
}
int main()
{
int year = 0;
printf("请输入要判断的年份:");
scanf("%d",&year);
int flag = Is_leapyear(year);
if(flag)
printf("%d年是闰年\n",year);
else
printf("%d年不是闰年\n",year);
return 0;
}