- 使用if…else
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "输入一个整数:" ;
cin>>n;
if(n%2==0)
cout << n << "为偶数" << endl;
else
cout << n << "为奇数" << endl;
system("pause");
return 0;
}
- 使用三元运算符
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "输入一个整数:" ;
cin>>n;
(n%2==0) ? cout << n << "为偶数。" : cout << n << "为奇数。" << endl;
system("pause");
return 0;
}