C++——写一程序,判断键盘输入的某一年是否闰年。

  • 闰年的条件是符合下面二者之一: ①能被4整除,但不能被100整除,如2008 ②能被400整除,如2000
    方式一
#include <iostream>
using namespace std;
int main()
{
    
    
 int a;
 cout<<"请输入年份"<<endl;
 cin>>a;
 if(a%4==0&&a%100!=0||a%400==0)
    cout<<"闰年"<<endl;
 else
    cout<<"不是闰年"<<endl;
}

方式二

#include <iostream>
using namespace std;
int main()
{
    
    int y;
cin>>y;
if(y%4==0)
{
    
    
    if(y%100==0)
    {
    
    
    if(y%400==0)
    cout<<"IsLeapYear";
    else
    cout<<"NotLeapYear";
    }
    else cout<<"IsLeapYear";
    }
    else cout<<"NotLeapYear";
}

猜你喜欢

转载自blog.csdn.net/qq_41017444/article/details/104543547