用C语言编写程序,输出2000年到3000年之间的所有闰年,每输出10个年号换一行。(能被4整除但不能被100整除,或者被400整除的年份)(for语句)
时间: 2024-12-19 13:28:14 浏览: 89
在C语言中,你可以使用for循环来遍历指定范围内的年份,并根据闰年的条件判断是否打印。闰年的规则是:能够被4整除但不能被100整除,或者能够被400整除。以下是相应的代码示例:
```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() {
for (int year = 2000; year <= 3000; year++) {
if (is_leap_year(year)) { // 判断是否是闰年
printf("%d ", year); // 输出闰年年份
if ((year - 2000) % 10 == 0) { // 每输出10个年号换一行
printf("\n");
}
}
}
return 0;
}
相关问题
用C语言编写程序输出1900-2000年中所有的闰年。每三个年号换一行。
在C语言中,我们可以使用`for`循环以及判断闰年的条件来输出这个范围内的所有闰年。闰年的规则是:能被4整除的年份通常是闰年,但是能被100整除的年份不是闰年;然而,如果一个年份能够被400整除,它仍然是闰年。
下面是简单的C代码实现:
```c
#include <stdio.h>
// 判断是否为闰年的函数
int is_leap_year(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
return 1;
else
return 0;
} else
return 1; // 能被4整除且不能被100整除
} else
return 0; // 不能被4整除
}
int main() {
int start = 1900;
int end = 2001; // 注意这里为了包含2000年,end设为2001
for (int year = start; year <= end; year++) {
if (is_leap_year(year)) {
printf("%d ", year);
if ((year + 2) % 3 == 0) { // 每隔三年换行
printf("\n");
}
}
}
return 0;
}
```
用c语言编写一个程序,输出1900-2000年中所有的闰年。每输出3个年号换一行。
好的,下面是用C语言编写的程序,可以实现输出1900-2000年中所有的闰年,并且每输出3个年号换一行:
```c
#include <stdio.h>
int main()
{
int year, count = 0;
for (year = 1900; year <= 2000; year++)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
printf("%d ", year);
count++;
if (count % 3 == 0)
{
printf("\n");
}
}
}
printf("\n");
return 0;
}
```
程序的思路是循环遍历1900年到2000年之间的每一年,判断该年是否为闰年,如果是则输出该年,并且计数器加一。每输出三个年号后换行。最后输出一个换行符,保证程序的输出格式正确。
阅读全文
相关推荐














