C++语言简单算术运算符示例
C++ 有 5 个基本的算术运算符。它们是:
- 加法(+)
- 减法(-)
- 除法(/)
- 乘法(*)
- 取模(%)
这些运算符可以在 C++ 中对任何算术运算进行操作。我们来看一个示例:
示例
#include <iostream> using namespace std; main() { int a = 21; int b = 10; int c ; c = a + b; cout << "Line 1 - Value of c is :" << c << endl ; c = a - b; cout << "Line 2 - Value of c is :" << c << endl ; c = a * b; cout << "Line 3 - Value of c is :" << c << endl ; c = a / b; cout << "Line 4 - Value of c is :" << c << endl ; c = a % b; cout << "Line 5 - Value of c is :" << c << endl ; return 0; }
输出
将输出以下内容:
Line 1 - Value of c is :31 Line 2 - Value of c is :11 Line 3 - Value of c is :210 Line 4 - Value of c is :2 Line 5 - Value of c is :1
广告