C语言判断是否为闰年的程序

该代码示例展示了如何使用C语言编写程序来判断输入的年份是否为闰年,并在非闰年的情况下预测下一次的闰年。程序通过定义闰年的条件,即年份能被4整除但不能被100整除,或者能被400整除,实现了这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据闰年的定义,如果年份能被4整除但不能被100整除,或者能被400整除,则为闰年。
第一版代码如下:

#include <stdio.h>

int main(){
	
	int year;
	printf("请输入公元年份(Please enter year AD):");
	scanf("%d", &year);
	/*根据闰年的定义,如果年份能被4整除但不能被100整除,或者能被400整除,则为闰年。*/
	if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
		printf("公元 %d 年是闰年!\n", year);
	}	
	else{
		printf("公元 %d 年不是闰年!\n", year);
	}
		
		
	return 0; 
} 

运行结果:

请输入公元年份(Please enter year AD):2023
公元 2023 年不是闰年!

--------------------------------
Process exited after 3.784 seconds with return value 0
请按任意键继续. . .

为了增加趣味性,也可以增加一些功能,比如在上面代码的基础上,如果不是闰年,预言下一次闰年是哪一年?
第二版代码如下:

#include <stdio.h>

#define  CONDITION  ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) //判断条件 

//输出下一次闰年的函数 
int forecast(int year){
	while(!CONDITION){
		year++;
	}
	
	return year;
}


int main(){
	
	int year;
	int lyear;
	printf("请输入公元年份(Please enter year AD):");
	scanf("%d", &year);
	/*根据闰年的定义,如果年份能被4整除但不能被100整除,或者能被400整除,则为闰年。*/
	if(CONDITION){
		printf("公元 %d 年是闰年!\n", year);
	}	
	else{
		printf("公元 %d 年不是闰年!\n", year);
		lyear = forecast(year);//输出下一次闰年 
		printf("公元 %d 年是下一次闰年!\n", lyear);
		
	}
		
		
	return 0; 
} 

运行结果如下:

请输入公元年份(Please enter year AD):2021
公元 2021 年不是闰年!
公元 2024 年是下一次闰年!

--------------------------------
Process exited after 3.13 seconds with return value 0
请按任意键继续. . .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九层指针

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值