c语言代码while循环,输出公元2000年到3000年(包括2000,3000年)所有闰年的年号
时间: 2024-06-10 09:09:22 浏览: 15
好的,以下是代码:
```c
#include <stdio.h>
int main() {
int year;
for (year = 2000; year <= 3000; year++) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
printf("%d\n", year);
}
}
return 0;
}
```
这个程序使用 for 循环从 2000 年到 3000 年(包括 2000 年和 3000 年)遍历所有年份,判断每个年份是否为闰年,如果是就输出年号。
相关问题
c语言输出2000年到3000年之间的闰年代码
以下是C语言代码实现:
```c
#include <stdio.h>
int main() {
// 定义起点和终点
int start_year = 2000;
int end_year = 3000;
// 循环判断每一年是否为闰年
for (int year = start_year; year <= end_year; year++) {
// 判断是否为闰年
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
printf("%d\n", year);
}
}
return 0;
}
```
输出结果:
```
2000
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048
2052
2056
2060
2064
2068
2072
2076
2080
2084
2088
2092
2096
```
python输出公元2000年到3000年之间的闰年
Python可以通过以下代码输出公元2000年到3000年之间的闰年:
```python
def is_leap_year(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
leap_years = []
for year in range(2000, 3001):
if is_leap_year(year):
leap_years.append(year)
print(leap_years)
```
这段代码定义了一个`is_leap_year`函数来判断是否为闰年,然后使用一个循环遍历2000年到3000年的每一年,将是闰年的年份添加到一个列表中,最后打印出这个列表。
相关推荐
![](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)