温馨提示×

c语言中怎么判断闰年

小亿
205
2023-09-09 12:10:31
栏目: 编程语言

在C语言中,可以使用以下方法判断闰年:

  1. 若该年份能被4整除但不能被100整除,则为闰年;

  2. 若该年份能被400整除,则为闰年;

  3. 其他情况下均为平年。

下面是一个示例代码:

#include <stdio.h>
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;
}

运行程序后,根据输入的年份会判断其是否为闰年,并输出相应的结果。

0