打印一下1000至2000之间的闰年
初始编程如下:
/*闰年 能被4整除但是不能被100整除的
或者能被400整除的是(世纪)闰年
所以做出如下程序:*/
#include<stdio.h>
#include<stdlib.h>
main()
{
int x=1000;
while (1000 <= x <= 2000)//此时计算机首先执行1000是否<=1000 满足 条件为真 即为1 。在判断1是否<=2000 为真,则条件一直成立,x一直执行自增。所以出现错误。
{
x++;
if ((x % 4 == 0 && x % 100 != 0) || x % 400 == 0)
printf("%d\n", x);
}
system("pause");
return 0;
}
报错,
做出改正:
while (x>=1000&&x<=2000)
其实此处用for循环更为简单不易出错
int x=1000
for(x,x<=2000,x++)//只判定右界限即可。
/*闰年 能被4整除但是不能被100整除的
或者能被400整除的是(世纪)闰年
所以做出如下程序:*/
#include<stdio.h>
#include<stdlib.h>
main()
{
int x=1000;
for (x; x <= 2000; x++)
{
if ((x % 4 == 0 && x % 100 != 0) || x % 400 == 0);
printf("%d\n", x);
}
system("pause");
return 0;
}