c语言判断闰年pta
时间: 2023-12-12 09:35:11 浏览: 44
以下是C语言判断闰年的代码示例:
```c
#include <stdio.h>
int main() {
int n, year;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}
```
该代码通过输入测试组数n和n个年份y,判断每个年份y是否为闰年,若是则输出YES,否则输出NO。
相关问题
c语言判断闰年流程图
判断闰年的方法是:能被4整除但不能被100整除,或者能被400整除的年份是闰年。
以下是C语言的判断闰年的代码示例:
```c
#include <stdio.h>
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;
}
```
以下是C语言的判断闰年的流程图示例:
![判断闰年的流程图](https://img-blog.csdn.net/20180419103212909?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hpbmFfY2hpbmFfY2hpbmFfY2hpbmFfY2hpbmFfY2hpbmFfY2hpbmFfY2hpbmFfY2hpbmFfY2hpbmFfY2hpbmFfY2hpbmFfY2hpbmFfYw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/80/format/webp)
用c语言判断闰年
可以使用以下的代码来判断一个年份是否是闰年:
```c
#include <stdio.h>
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 语句判断该年份是否为闰年,最后输出结果。其中,判断闰年的方法是:
1. 能被4整除且不能被100整除的年份是闰年;
2. 能被400整除的年份也是闰年。
根据这个方法,程序可以正确地判断一个年份是否为闰年。