c#算术运算符/
算术运算符 (Arithmetic Operators)
Arithmetic operators are the special symbols that are used for the Arithmetic / Mathematical operations. These operators can be unary and binary.
算术运算符是用于算术/数学运算的特殊符号。 这些运算符可以是一元和二进制。
Following are the types of Arithmetic Operators,
以下是算术运算符的类型 ,
Arithmetic unary operators
一元算术运算符
Arithmetic binary operators
算术二进制运算符
一元算术运算符 (Arithmetic unary operators)
For the unary operations – we need only one operand. These are the operators,
对于一元运算,我们只需要一个操作数。 这些是操作员,
Unary plus (+)
一元加(+)
Unary minus (-)
一元减(-)
Syntax:
句法:
+a
-a
Example:
例:
Input:
int a = -10;
Operation & Output:
+a = -10
-a = 10
C++ program to demonstrate the example of arithmetic unary operators
C ++程序演示算术一元运算符的示例
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = -10;
// printing the values
cout << "a: " << a << endl;
cout << "b: " << b << endl;
// unary plus operations
cout << "+a: " << +a << endl;
cout << "+b: " << +b << endl;
// unary minus operations
cout << "-a: " << -a << endl;
cout << "-b: " << -b << endl;
return 0;
}
Output:
输出:
a: 10
b: -10
+a: 10
+b: -10
-a: -10
-b: 10
算术二进制运算符 (Arithmetic binary operators)
For the binary operations – we need two operands. These are the operators,
对于二进制运算–我们需要两个操作数。 这些是操作员,
Operator | Name | Description |
---|---|---|
+ | Plus operator | Returns the addition of two operands. |
- | Minus operator | Returns the subtraction of two operands. |
* | Multiplication operator | Returns the multiplication (product) of two operands. |
/ | Divide operator | Returns the result of the division of two operands i.e. returns the quotient of the division operation. |
% | Modulus operator | Returns the remainder of the division operation on two operands. |
操作员 | 名称 | 描述 |
---|---|---|
+ | 加号运算符 | 返回两个操作数的加法。 |
-- | 减号 | 返回两个操作数的减法。 |
* | 乘法运算符 | 返回两个操作数的乘积(乘积)。 |
/ | 除法运算符 | 返回两个操作数的除法结果,即返回除法运算的商。 |
% | 模算子 | 返回两个操作数上除法运算的其余部分。 |
Syntax:
句法:
a + b
a - b
a * b
a / b
a % b
Example:
例:
Input:
int a = 10;
int b = 3;
Operation & Output:
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
C++ program to demonstrate the example of arithmetic binary operators
C ++程序演示算术二进制运算符的示例
#include <iostream>
#include <cmath> // for fmod() func.
using namespace std;
int main()
{
int a = 10;
int b = 3;
// printing the values
cout << "a : " << a << endl;
cout << "b : " << b << endl;
// arithmetic operations
cout << "a + b : " << a + b << endl;
cout << "a - b : " << a - b << endl;
cout << "a * b : " << a * b << endl;
cout << "a / b : " << a / b << endl;
cout << "a % b : " << a % b << endl;
cout << endl;
float x = 10.23f;
float y = 3.10f;
// printing the values
cout << "x : " << x << endl;
cout << "y : " << y << endl;
// arithmetic operations
cout << "x + y : " << x + y << endl;
cout << "x - y : " << x - y << endl;
cout << "x * y : " << x * y << endl;
cout << "x / y : " << x / y << endl;
// % operator doesn't work with float values
// use fmod() for this
// cout << "x % y : " << x % y << endl;
cout << "fmod(" << x << " , " << y << ") : " << fmod(x, y) << endl;
return 0;
}
Output:
输出:
a : 10
b : 3
a + b : 13
a - b : 7
a * b : 30
a / b : 3
a % b : 1
x : 10.23
y : 3.1
x + y : 13.33
x - y : 7.13
x * y : 31.713
x / y : 3.3
fmod(10.23 , 3.1) : 0.93
Recommended posts
推荐的帖子
What is the value of sizeof('x') and type of character literals in C++?
翻译自: https://www.includehelp.com/cpp-tutorial/arithmetic-operators-in-c-cpp.aspx
c#算术运算符/