判断一年是否是闰年的两个规则
1:能被被4整除但不能被100整除
2:能被400整除
只要满足其中之一就是闰年
根据上述条件便可写出判断平年闰年的代码
方法一:
#include<stdio.h>
int main()
{
int year;
printf("please enter a year:\n");
scanf("%d",&year);
if((year%400==0)||(year%4==0&&year%100!=0))
printf("this year is a leap year\n");
else
printf("this year is a non-leap year\n");
return 0;
}
方法二:调用函数方法
#include<stdio.h>
int cheak_year(int year);
int main()
{
int year;
printf("please enter a year:\n");
scanf("%d",&year);
cheak_year(year);
return 0;
}
int cheak_year(int year)
{
if(year % 4 == 0)
{
if(year % 100 != 0)
printf("this year is a leap year\n");
else if(year % 400 == 0)
printf("this year is a leap year\n");
}
else
{
printf("this year is a non-leap year\n");
}
return 0;
}
如有错误或简便方法欢迎指正 😃