一、
用if else语句:
1:能被4整除,不能被100整除。
2:能被4整除,也能被100整除。(能被400整除)
#include <stdio.h>
#include<iostream>
using namespace std;
void main()
{
int iYear = 0;
cout << "请输入年份" << endl;
cin >> iYear;
if (iYear % 4 == 0)
{
if (iYear % 100 == 0)
{
if (iYear % 400 == 0)
cout << "该年是闰年" << endl;
else
cout << "该年不是闰年" << endl;
}
else
cout << "该年不是闰年" << endl;
}
else
cout << "该年不是闰年" << endl;
system("pause");
}
二:用逻辑或和逻辑与
(iYear%4==0&&iYear%100==0)||(iYear%400==0)
#include<stdio.h>
#include<iostream>
using namespace std;
void main()
{
int iYear;
cout << "请输入年份" << endl;
cin >> iYear;
if ((iYear % 4 == 0 && iYear % 100 == 0) || iYear % 400 == 0)
cout << "该年是闰年" << endl;
else
cout << "该年不是闰年" << endl;
system("pause");
}
运行结果: