如何使用 C 语言查找闰年?

cserver side programmingprogramming更新于 2025/5/15 22:07:17

闰年是一年有 366 天。每四年,我们都会经历一个闰年。

我们将实现的逻辑是查找用户通过控制台输入的年份是否为闰年 −

if (( year%400 == 0)|| (( year%4 == 0 ) &&( year%100 != 0)))

如果满足此条件,则给定的年份为闰年。否则,则不是。

示例

以下是使用 If 条件检查闰年的 C 程序 −

#include <stdio.h>
int main(){
   int year;
   printf("Enter any year you wish 
");    scanf(" %d ", &year);    if (( year%400 == 0)|| (( year%4 == 0 ) &&( year%100 != 0)))       printf("
%d is a Leap Year.
", year);    else       printf("
%d is not the Leap Year.
", year);    return 0; }

输出

当执行上述程序时,它会产生以下结果 −

Enter any year you wish
2045
2045 is not the Leap Year.

相关文章