c语言 闰年的计算方法为①能被4整除但不能被100整除,②能被100整除且能被400整除,请输出1800~2100年中是闰年的年份。要求1800和2100的值由用户输入(即在屏幕上输入),并考虑如果输入年份不合逻辑,请给出提示,并重新输入。
#include <stdio.h>
int main()
{
int year;
while(1)
{
printf("请输入年份(1—9999):");
scanf("%d",&year);
if(year >0 && year <=9999)
{
break;
}
}
if(year%400==0||(year%4==0&&year%100!=0))
{
printf("%d是闰年\n",year);
}
else
{
printf("%d不是闰年\n",year);
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2016-10-19
#include<stdio.h>
int main(int argc, char **argv) {
int startY = 0;
int endY = 0;
int i = 0;
printf("Please Input start and end year [a,b]: ");
scanf("%d,%d", &startY, &endY);
if (endY < startY) {
printf("Input error.\n");
return -1;
}
if (endY < 0 || startY < 0) {
printf("Input Error.\n");
return -1;
}
for (i = startY; i <= endY; i++) {
if ((0 == i % 4 && 0 != i % 100) ||
(0 == i % 100 && 0 == i % 400))
{
printf("the [%d] is Leap year.\n", i);
}
}
return 0;
}
// 输出
ase Input start and end year [a,b]: 2000,2016
the [2000] is Leap year.
the [2004] is Leap year.
the [2008] is Leap year.
the [2012] is Leap year.
the [2016] is Leap year.追问
int main(int argc, char **argv) {
int startY = 0;
int endY = 0;
int i = 0;
printf("Please Input start and end year [a,b]: ");
scanf("%d,%d", &startY, &endY);
if (endY < startY) {
printf("Input error.\n");
return -1;
}
if (endY < 0 || startY < 0) {
printf("Input Error.\n");
return -1;
}
for (i = startY; i <= endY; i++) {
if ((0 == i % 4 && 0 != i % 100) ||
(0 == i % 100 && 0 == i % 400))
{
printf("the [%d] is Leap year.\n", i);
}
}
return 0;
}
// 输出
ase Input start and end year [a,b]: 2000,2016
the [2000] is Leap year.
the [2004] is Leap year.
the [2008] is Leap year.
the [2012] is Leap year.
the [2016] is Leap year.追问
请问int main 括号里面是什么 我们现在括号里都没东西的
请问int main 括号里面是什么 我们现在括号里都没东西的
追答可以接受输入参数用的,可以为空!~
第2个回答 2016-10-19
year%400==0 || (year%100!=0 && year%4==0)