Q1. What are arithmetic operators in C++? Distinguish between unary and binary arithmetic operators. Give examples for each of them.
Ans. C++ provides operators for five basic arithmetic calculations: addition, subtraction, multiplication, division and remainder which are +,-,*,and %.
Operators that act on one operand are referred to as Unary Operators.
- Unary +
The operator unary ‘+’ precedes an operand. The operand (the value on which the operator operates) of the unary + operator must have arithmetic or pointer type and the result is the value of the argument.
If a=5 then +a means 5.
- Unary –
The operator unary – precedes an operand. the operand of the unary – operator must have arithmetic type and the result is the negation of its operand’s value.
If a=5 then –a means -5.
Operators that act upon two operands are referred to as Binary Operators.
- Addition operator +
The arithmetic binary operator + adds values of its operands and the result is the sum of the values of its two operands.
4+20 results in 24.
- Subtraction operator –
The – operator subtracts the second operand from the first.
14-3 evaluates to 11.
- Multiplication operator *
The * operator multiplies the values of its operands. For example,
3 * 4 evaluates to 12.
- Division operator /
The / operator divides its first operand by the second.
100 / 5 evaluates to 20.
- Modulus operator %
The % operator finds the modulus of its operand relative to the second. That is, it produces the remainder of dividing the first by the second operand.
While using modulus operator both operands must be integer types.
19 % 6 evaluates to 1, since 6 goes into 19 three times with a remainder 1.