编辑实现,1-2500年份是否为闰年(至少用三种方案)使用c++编写
时间: 2023-02-13 08:03:58 浏览: 55
在 C 语言中,可以使用以下三种方法来判断一个年份是否为闰年:
1. 使用条件运算符
这种方法适用于所有的年份(包括 1 到 2500 年)。
```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;
}
```
2. 使用 switch 语句
这种方法仅适用于 1 到 2500 年。
```c
#include <stdio.h>
int main() {
int year;
// 读入年份
printf("请输入年份: ");
scanf("%d", &year);
// 判断是否为闰年
switch (year % 4) {
case 0:
if (year % 100 == 0) {
if (year % 400 == 0) {
printf("%d 是闰年\n", year);
} else {
printf("%d 不是闰年\n", year);
}
} else {
printf("%d 是闰年\n", year);
}
break;
default:
printf("%d 不是闰年\n", year);
break;
}
return 0;
}
```
3. 使用函数
这种方法适用于所有的年份(包括 1 到 2500 年)。
```c
#include <stdio.h>
// 判断是否为闰年的函数
int is_leap_year(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return 1; // 闰年
} else {
return 0; // 不是闰年
}
}
int main() {
int year;
// 读入年份
printf("请输入年份: ");
scanf("
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)