帮我设计一个判断年份是否为闰年的c++程序
时间: 2024-05-03 16:23:23 浏览: 13
以下是一个判断年份是否为闰年的C程序:
```c
#include <stdio.h>
int main() {
int year;
printf("请输入一个年份:");
scanf("%d", &year);
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
printf("%d年是闰年。\n", year);
else
printf("%d年不是闰年。\n", year);
} else
printf("%d年是闰年。\n", year);
} else
printf("%d年不是闰年。\n", year);
return 0;
}
```
程序先要求用户输入一个年份,然后通过对4、100和400取模的结果来判断是否为闰年,最终输出结果。
相关问题
用c++写一个程序判断输入年份是否为闰年
以下是用 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;
}
```
只需要输入一个年份,程序就会输出该年份是否是闰年。如果是闰年,会输出“X年是闰年”;如果不是闰年,会输出“X年不是闰年”,其中 X 表示用户输入的年份。
写一个判断是否为闰年的c++程序
以下是判断是否为闰年的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;
}
```
该程序通过判断年份是否能被4整除且不能被100整除,或者能被400整除来判断是否为闰年。