判定2000-2500中每一年是否为闰年
1、能被4整除,但不能被100整除
2、能被400整除的
供参考:
#include <stdio.h>
int main()
{
int i;
for (i = 2000; i <= 2500; i++) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)//闰年判断条件
printf("%d is a leap year!\n", i);
else
printf("%d is not a leap year!\n", i);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void input_year(int y[], int n, int sy);
void leap_year(int y[], int n);
int main()
{
int year[501]; // Define the array, because the year is from 2000 to 2500, so the array subscript is 501.
input_year(year, 501, 2000);
leap_year(year, 501);
system("pause");
return 0;
}
void input_year(int y[], int n, int sy)
{
int i;
for(i=0; i<n; i++)
y[i]=sy++;
}
void leap_year(int y[], int n)
{
int i;
for(i=0; i<n; i++){
if ((y[i]%4==0&&y[i]%100!=0)||y[i]%400==0)
printf("%d is a leap year!\n", y[i]);
else
printf("%d is not a leap year!\n", y[i]);
}
}
int y;
for(y=2000;y<=2500;y++)
{
if(y%4==0 && y%100!=0 || y%400==0)//是闰年
else //不是闰年
}
上面的回答每一个数字是不是闰年都输出,太繁琐了,打印的信息多,不是很友好,你可以看下面的代码:
#include <stdio.h>
int main()
{
int i = 0; // 初始化
int count = 0; // 记录闰年数量和换行
printf("The following numbers are leap years:\n");
for (i = 2000; i <= 2500; i++) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) { // 闰年判断条件
printf("%d\t", i);
count++;
if (count % 7 == 0) // 分成7个每行,排版上更好看
printf("\n");
}
}
return 0;
}