该程序检查输入年份是否为闰年。
示例:检查闰年的程序
您可以使用以下数学逻辑检查一年是否是闰年:
闰年:
如果一年可以被 4, 100 和 400 整除,则它是闰年。
如果一年可以被 4 整除但不能被 100 整除那么它就是闰年
不是闰年:
如果一年不能被 4 整除那么它不是闰年
如果一年可被 4 和 100 整除但不能被 400 整除那么它就不是闰年
让我们在 C 程序中编写这个逻辑。要了解该程序,您应该具有以下 C 编程主题的知识:
C 编程if..else,嵌套if..else#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;}输出:
Enter year: 19911991 is not a Leap Year
看看这些相关的 C 程序:
