Arithmetic Operators in C ++
Arithmetic Operators in C++
Arithmetic operators are used to perform mathematical calculations on numerical values (operands) in C++. These operators include:
* Addition (+): Adds two operands.
int a = 5, b = 3;
int sum = a + b; // sum will be 8
* Subtraction (-): Subtracts the second operand from the first.
int a = 10, b = 4;
int difference = a - b; // difference will be 6
* Multiplication (*): Multiplies two operands.
int a = 2, b = 6;
int product = a * b; // product will be 12
* Division (/): Divides the first operand by the second.
int a = 15, b = 3;
int quotient = a / b; // quotient will be 5
* Modulus (%): Returns the remainder of the division between two operands.
int a = 7, b = 3;
int remainder = a % b; // remainder will be 1
* Increment (++): Increases the value of an operand by 1.
int a = 5;
a++; // a will be 6
* Decrement (--): Decreases the value of an operand by 1.
int a = 5;
a--; // a will be 4
Operator Precedence
When multiple operators are used in an expression, the order of evaluation is determined by operator precedence. The order is as follows:
* Parentheses ()
* Increment/decrement ++, -- (prefix)
* Multiplication/division/modulus *, /, %
* Addition/subtraction +, -
* Increment/decrement ++, -- (postfix)
Example:
int result = 2 + 3 * 4; // result will be 14 (multiplication has higher precedence)
By understanding arithmetic operators and their precedence, you can effectively perform calculations and manipulate numerical values in your C++ programs.
Comments
Post a Comment