c#算术运算符/_C / C ++中的算术运算符

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,

对于一元运算,我们只需要一个操作数。 这些是操作员,

  1. Unary plus (+)

    一元加(+)

  2. 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,

对于二进制运算–我们需要两个操作数。 这些是操作员,

OperatorNameDescription
+Plus operatorReturns the addition of two operands.
-Minus operatorReturns the subtraction of two operands.
*Multiplication operatorReturns the multiplication (product) of two operands.
/Divide operatorReturns the result of the division of two operands i.e. returns the quotient of the division operation.
%Modulus operatorReturns 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

推荐的帖子

翻译自: https://www.includehelp.com/cpp-tutorial/arithmetic-operators-in-c-cpp.aspx

c#算术运算符/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值