程序检查给定年份是否为C中的leap年
year年有366天,而正常年份有365天,任务是通过程序检查给定年份是否为a年。
它的逻辑可以通过检查是否将Year除以400或4,但是如果数字未除以正常年份而得到的任何数字。
示例
Input-: year=2000 Output-: 2000 is a Leap Year Input-: year=101 Output-: 101 is not a Leap year
算法
Start
Step 1 -> declare function 布尔检查年份是否为a年
bool check(int year)
IF year % 400 = 0 || year%4 = 0
return true
End
Else
return false
End
Step 2 -> In main() Declare variable as int year = 2000
Set check(year)? printf("%d is a Leap Year",year): printf("%d is not a Leap Year",year)
Set year = 10
Set check(year)? printf("%d is a Leap Year",year): printf("\n%d is not a Leap Year",year);
Stop示例
#include <stdio.h>
#include <stdbool.h>
//布尔检查年份是否为a年
bool check(int year){
//如果一年是400的倍数或4的倍数,则它是a年
if (year % 400 == 0 || year%4 == 0)
return true;
else
return false;
}
int main(){
int year = 2000;
check(year)? printf("%d is a Leap Year",year): printf("%d is not a Leap Year",year);
year = 101;
check(year)? printf("%d is a Leap Year",year): printf("\n%d is not a Leap Year",year);
return 0;
}输出结果
2000 is a Leap Year 101 is not a Leap Year