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

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

#include 

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 

#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
请按任意键继续. . .

你可能感兴趣的:(C语言,c语言,开发语言)