C++ 实例 - 判断一个数是奇数还是偶数
C++ 教程
· 2019-02-26 11:26:55
以下我们使用 % 来判断一个数是奇数还是偶数,原理是,将这个数除于 2 如果余数为 0 为偶数,否则Wie奇数。
实例 - 使用 if...else
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "输入一个整数: ";
cin >> n;
if ( n % 2 == 0)
cout << n << " 为偶数。";
else
cout << n << " 为奇数。";
return 0;
}
以上程序执行输出结果为:
输入一个整数: 5 5 为奇数。
实例 - 使用三元运算符
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "输入一个整数: ";
cin >> n;
(n % 2 == 0) ? cout << n << " 为偶数。" : cout << n << " 为奇数。";
return 0;
}
以上程序执行输出结果为:
输入一个整数: 5 5 为奇数。
点击查看所有 C++ 教程 文章: https://www.codercto.com/courses/l/18.html
Remote
Jason Fried、David Heinemeier Hansson / Crown Business / 2013-10-29 / CAD 26.95
The “work from home” phenomenon is thoroughly explored in this illuminating new book from bestselling 37signals founders Fried and Hansson, who point to the surging trend of employees working from hom......一起来看看 《Remote》 这本书的介绍吧!