判断是否为闰年(bool)

时间:2023-03-09 18:24:45
判断是否为闰年(bool)

bool为布尔型,只有一个字节,取值false和true

#include<iostream>
using namespace std;
int main()
{
 int year;
 bool leap;                                 //bool为布尔型,只有一个字节,取值false和true
 cout<<"please enter year:";
 cin>>year;
 if(year%4==0)
 {
  if(year%100==0)
  {
   if(year%400==0)
    leap=true;                      //是闰年
   else
    leap=false;                     //不是闰年
  }
  else
   leap=true;
 }
 else
  leap=false;
 if(leap)                                   //判断leap的值
  cout<<year<<" is a leap year!"<<endl;   //leap=true
 else
  cout<<year<<" is not a leap year!"<<endl;   //leap=false
 return 0;
}