c语言闰年编程怎么写

时间:2025-01-23 17:01:30 游戏攻略

在C语言中,判断一个年份是否为闰年可以通过以下逻辑实现:

1. 如果年份能被4整除但不能被100整除,则为普通闰年。

2. 如果年份能被400整除,则为世纪闰年。

```c

include

int main() {

int year;

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;

}

```

这个程序首先提示用户输入一个年份,然后使用`if`语句来判断该年份是否符合闰年的条件,并输出相应的结果。