判断任意年份是否为闰年,需要满足以下条件中的任意一个:
① 该年份能被 4 整除同时不能被 100 整除;
② 该年份能被400整除。
#include <stdio.h>
int main()
{
int y;
printf("Enter year: ");
scanf("%d",&y);
if(y % 4 == 0)
{
//Nested if else
if( y % 100 == 0)
{
if ( y % 400 == 0)
printf("%d is a Leap Year", y);
else
printf("%d is not a Leap Year", y);
}
else
printf("%d is a Leap Year", y );
}
else
printf("%d is not a Leap Year", y);
return 0;
}
Results:
|
1 2 |
Enter year: 2045 2045 is not a Leap Year |
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!