【C++】判断某一年是否为闰年

这是一个C++程序,用于判断输入的年份是否为闰年。程序首先定义了一个逻辑变量`leap`,然后根据年份能否被4、100和400整除的条件来判断。如果年份可以被4整除且不能被100整除,或者能被400整除,那么该年份是闰年。程序会输出相应的结果。

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

问题描述

编写程序,判断某一年是否为闰年。

编写程序(例)

//判断某一年是否为闰年 
#include <iostream>
using namespace std;
int main()
{
	int year;
	bool leap;							//定义逻辑变量(bool型 )
	cout<<"please enter year:";			//提示输入年份 
	cin>>year;							//输入年份 
	if(year %4 == 0)					//年份能被4整除 
	{								
		if(year %100 == 0)				//年份能被4整除 又年份能被100整除  
		{
			if(year %400 == 0)			//年份能被4整除 又年份能被400整除 
				leap=true;				//闰年,令leap=ture 
			else
				leap=false;				//非闰年,令leap=false 
		}
		else
			leap=true;
	}
	else
		leap=false;						//年份不能被4整除,不是闰年 
	if(leap)
		cout<<year<<" is a leap year!"<<endl;	//leap为真,输出年份是闰年 
	else
		cout<<year<<" is not a leap year!"<<endl;//leap为假,输出年份不是闰年 
	
	return 0;
		
}

运行结果(例)

please enter year:2004
2004 is a leap year!
--------------------------------
Process exited after 4.59 seconds with return value 0
请按任意键继续. . .


please enter year:2005
2005 is not a leap year!
--------------------------------
Process exited after 3.354 seconds with return value 0
请按任意键继续. . .

喜欢的朋友点赞加关注哦!欢迎评论,给出更好的建议和意见哦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值