An Operator is a symbol which behaves like a function, that tells the computer or the compiler to perform some mathematical operations on variable and values. Operators are used in programming in order to manipulate data and variables. C++ has a predefined set of different Operators. One such commonly used Operator is the Arithmetic Operators. Let us learn about it in detail.
Definition
In C++, Arithmetic Operators are symbols used to perform common arithmetic operations like addition, subtraction, multiplication, division, modulus, etc.
Arithmetic Operators are operators which are used within the equation to perform a number of basic mathematical calculations.
Arithmetic Operators
Arithmetic Operators use a specific symbol in order to be used within the program. Arithmetic Operators use two operands and an operator between them in order to calculate the result. Arithmetic Operators are simply sign’s which we use in the mathematical equations in order to compute results.
Basic Arithmetic Operators in C++ are listed below:
Operator | Meaning | Example |
+ | Addition of variables/values | 5 + 10 = 15 |
– | Subtraction | 10 – 4 = 6 |
* | Multiplication | 3 * 20 = 60 |
/ | Division | 40 / 5 = 8 |
% | Modulus which indicates remainder in a division operator | 40 % 10 = 0 |
Although the Arithmetic Operators are often used to calculate results between 2 values, they can also be used between 2 variables.
Let us understand this by taking an example:
int a = 10 - 4 ; // a = 6int b = a * 3 ; // b = 18int c = a + b ; // c = 24
Let’s look at each Arithmetic Operator Separately
Addition Operator
Addition Arithmetic Operators are used to add 2 values and variables in a program statement. The symbol used for this type of operator is ‘+’. In simple words, Addition combines 2 numbers.
Example
int main(){int a, b;a = 10;b = 5;cout << "a + b = " << (a + b) << endl ;}
Output
a + b = 15
Browse all the Topics UnderOperator and Expressions: Operators
- Assignment Operator
- C++ Shorthands
- Unary Operator
- Increment and Decrement Operators
- Relation Operator
- Logical Operators
Subtraction Operator
Subtraction Arithmetic Operator is used to subtract 2 values or variables in the program. The symbol used for this operator is ‘-’.
Example
int main(){int a, b;a = 10;b = 5;cout << "a - b = " << (a - b) << endl ;}
Output
a - b = 5
Multiplication Operator
This operator is denoted by the ‘*’ or ‘x’ symbol. It gives the product of two numbers.
Example
int main(){int a, b;a = 10;b = 5;cout << "a * b = " << (a * b) << endl ;}
Output
a * b = 50
Division Operator
This Arithmetic Operator divides its first operand by the second operand. It is denoted by the ‘/’ symbol. In C++, if we divide An Integer with another Integer then we will get the Quotient as the result. But if either of the operands is a floating-point number, then the result of division will be in Decimals.
Example
In C++
int a = 7 / 2 ; // a = 3int b = 7.0 / 2 ; // b = 3.5int c = 7 / 2.0 ; // c = 3.5
Modulus Operator
This type of Arithmetic Operator computes the remainder of the two values when divided. The symbol used to define this operator is ‘%’. Both the operands must be integer type only. If they are of the floating-point type, then there might arise a compile-time error.
Example
int main(){int a = 12 ;int b = 5 ;cout << "c = " << (a % b) << endl ;}
Output
c = 2
Order of Operations
There can rise an issue when an operand has to be computed with 2 or more other operands at the same time. When more than one operator is applied to the same operand, C++ uses the rules of precedence to decide which operator actions to be applied first.
For example
int speed = 12 * 5 + 34 ;
Here the value of speed can be either 94 or 468.
In this above example, we can see that Value 5 is an operand for both the * and + operators. In this situation, C++ follows the Precedence Rules.
The Precedence Rules states that in such situations, the sequence to compute the value is multiplication, division, then modulus before the addition and subtraction mathematical operations.
FAQs onArithmetic Operators
Q1. What is the output of this program?
intmain(){inta;a = 10 + 2 * 5;cout <<a;return0;}
- 20
- 30
- 60
- 80
Answer. Option A. * operator has higher precedence than + operator.
Q2. What is the output of the following line of statement?
int count = 10.5 / 5 ;
- 2
- 0
- 1
- None of the above
Answer. Option C.
Q3. The operator that takes 2 operands is?
- Unary
- Binary
- Ternary
- None
Answer. Option B
Q4. Arithmetic Operators provided by C++ are?
- Modulus %
- Equate =
- Addition +
- Both A and C
Answer. Option D