在C语言中,可以通过以下方法判断一个年份是否为闰年:
普通闰年 :年份能够被4整除但不能被100整除。世纪闰年:
年份能够被400整除。
```c
include
int isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1; // 是闰年
} else {
return 0; // 不是闰年
}
}
int main() {
int year;
printf("请输入一个年份: ");
scanf("%d", &year);
if (isLeapYear(year)) {
printf("%d 是闰年。\n", year);
} else {
printf("%d 不是闰年。\n", year);
}
return 0;
}
```
代码解释:
函数声明:
`int isLeapYear(int year);` 声明了一个名为 `isLeapYear` 的函数,接受一个整数参数 `year`,并返回一个整数。
闰年判断逻辑
如果年份能被4整除且不能被100整除,返回1(是闰年)。
如果年份能被400整除,返回1(是闰年)。
其他情况返回0(不是闰年)。
主函数
提示用户输入一个年份。
调用 `isLeapYear` 函数判断输入的年份是否为闰年,并输出结果。
示例运行:
```
请输入一个年份: 2020
2020 是闰年。
```
通过上述代码,你可以轻松地判断任意一个年份是否为闰年。