ide工具:vs2017
代码如下
方法1:while循环编写
//用课本例2.3的N-S图判定闰年的算法
#include <stdio.h>
int main()
{
int year=2000;
while(year <= 2500)
{
if (year % 4 == 0) //year/4的余数为0的条件下
{
if (year % 100 == 0) //year/100的余数为0的条件下
{
if (year % 400 == 0) //year/400的余数为0的条件下
{
printf("%d是闰年\n", year);
year++;
}
else //year/400的余数不为0
{
printf("%d非闰年\n", year);
year++;
}
}
else //year/100的余数不为0
{
printf("%d是闰年\n", year);
year++;
}
}
else //year/4的余数不为0
{
printf("%d非闰年\n", year);
year++;
}
}
return 0;
}
方法2:for循环编写
//用课本例2.3的N-S图判定闰年的算法
#include <stdio.h>
int main()
{
int year;
for(year=2000;year<=2500;year++)
{
if (year % 4 == 0) //year/4的余数为0的条件下
{
if (year % 100 == 0) //year/100的余数为0的条件下
{
if (year % 400 == 0) //year/400的余数为0的条件下
{
printf("%d是闰年\n", year);
}
else //year/400的余数不为0
{
printf("%d非闰年\n", year);
}
}
else //year/100的余数不为0
{
printf("%d是闰年\n", year);
}
}
else //year/4的余数不为0
{
printf("%d非闰年\n", year);
}
}
return 0;
}