如何用 C 语言判断闰年?
闰年具有 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.
广告
数据结构
网络
关系型数据库
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP