Basic Arithmetic of C++

Arithmetic Operators: C++ supports these arithmetic operator.

Operator                Symbol              Form

Addition                  +                 x+y
Subtraction               -                 x-y
Multiplication            *                 x*y
Division                  /                 x/y
Modulus                   %                 x%y

Addition: The addition operator adds its operands together.

int x=2+3;
cout<<x;

Output:

5

Subtraction: The subtraction operator subtracts one operands from the other.

int x=5-3;
cout<<x;

Output:

2

Multiplication: The multiplication operator multiplies its operands.

int x=3*2;
cout<<x;

Output:

6

Division: The division operator divides the first operand by the second. Any remainder is dropped in order to return an integer value.

int x=10/3;
cout<<x;

Output:

3

If one or both of the operands are floating point values,the division operator performs floating point division.

Modulus: The modulus operator (%) is informally know as the remainder operator because it returns the remainder after an integer division.

int x=25%6;
cout<<x;

Output:

1

Operator Precedence: Operator precedence determines the grouping of terms in an expression,which affects how an expression is evaluated certain operators take higher precedence over others; for example, the multiplication operator has higher precedence over the addition operator.

For example:

int x=5+2*2;
cout<<x;

Output:

9

The program evaluates 2*2 first, and then adds the result 5.
As in mathematics,using parentheses alters operator precedence.

int x=(5+2)*2;
cout<<x;

Output:

14

Discussion

Read Community Guidelines
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.