C语言程序:输出21世纪(2001年至今)的闰年年份
以下是一个用于输出从2001年开始到用户输入年份之前的所有闰年年份的 C 语言程序:
#include <stdio.h>
int main()
{
int endYear;
printf("请输入一个年份:");
scanf("%d", &endYear);
int startYear = 2001;
int hasLeapYear = 0;
printf("21世纪中截至%d年之前的所有闰年年份为:", endYear);
for (int year = startYear; year < endYear; year++)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
printf("%d ", year);
hasLeapYear = 1;
}
}
if (!hasLeapYear)
{
printf("None");
}
printf("\n");
return 0;
}
在这个程序中,我们首先读取用户输入的年份,并定义了起始年份为2001和一个变量hasLeapYear来表示是否存在闰年。接着使用一个for循环遍历从2001年到用户输入年份之前的每一年。
在每次循环中,我们使用判断条件判断当前年份是否为闰年。如果是闰年,则输出该年份,并将hasLeapYear设为1表示存在闰年。
最后,我们在循环结束后检查hasLeapYear的值。如果为0,表示没有闰年,输出'None';否则,输出从2001年到输入年份之前的所有闰年年份。
希望这个程序能满足您的需求。如果您还有其他问题,请随时提问。
原文地址: https://www.cveoy.top/t/topic/bg0h 著作权归作者所有。请勿转载和采集!