设计算法并编写程序求出公元1~10000年中的所有闰年。使用C语言用函数实现。
以下是使用函数实现的C语言程序:
#include <stdio.h>
// 判断闰年的函数
int isLeapYear(int year)
{
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
// 输出闰年的函数
void printLeapYears(int startYear, int endYear)
{
int year;
printf("公元%d年到公元%d年之间的闰年有:\n", startYear, endYear);
for (year = startYear; year <= endYear; year++)
{
if (isLeapYear(year))
{
printf("%d\n", year);
}
}
}
int main()
{
printLeapYears(1, 10000);
return 0;
}
在上面的程序中,使用了两个函数:
isLeapYear():判断一个年份是否为闰年,如果是返回1,否则返回0。printLeapYears():输出指定区间内的所有闰年。
程序的主函数调用了printLeapYears()函数,输出公元1年到公元10000年中的所有闰年。printLeapYears()函数中使用了isLeapYear()函数来判断每个年份是否为闰年,如果是则输出年份
原文地址: https://www.cveoy.top/t/topic/cOkc 著作权归作者所有。请勿转载和采集!