条件:
1,能被4整除,并且不能被100整除
2,能被400整除。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main(void)
{
int leap, year;
printf("enter year:\n");
scanf("%d", &year);
if (year % 4==0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
leap = 1;
else
leap = 0;
}
else
leap = 1;
}
else
leap = 0;
if (leap)
printf("%d is", year);
else
printf("%d is not", year);
printf(" a leap year.\n");
return 0;
}
还有一种更简单的方法:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
int year, leap;
scanf("%d", &year);
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
leap = 1;
else
leap = 0;
if (leap)
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
return 0;
}