问题描述
编写程序,判断某一年是否为闰年。
编写程序(例)
//判断某一年是否为闰年
#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
请按任意键继续. . .
喜欢的朋友点赞加关注哦!欢迎评论,给出更好的建议和意见哦!