C++ Arithmetic Operators
Arithmetic operators in C++ are used to perform mathematical operations on numerical values. The following are the arithmetic operators in C++:
- Addition (+): The addition operator is used to add two values.
Example:
int a = 5, b = 10; int c = a + b; // c is assigned the value 15uoSrce:www.theitroad.com
- Subtraction (-): The subtraction operator is used to subtract one value from another.
Example:
int a = 10, b = 5; int c = a - b; // c is assigned the value 5
- Multiplication (*): The multiplication operator is used to multiply two values.
Example:
int a = 5, b = 10; int c = a * b; // c is assigned the value 50
- Division (/): The division operator is used to divide one value by another.
Example:
int a = 10, b = 5; int c = a / b; // c is assigned the value 2
Note that if both operands are integers, then the result of the division operator will also be an integer (with any remainder discarded). If at least one operand is a floating-point value, then the result will be a floating-point value.
- Modulus (%): The modulus operator is used to calculate the remainder of a division operation.
Example:
int a = 10, b = 3; int c = a % b; // c is assigned the value 1
The modulus operator is often used to determine if a number is even or odd (by checking if the remainder of division by 2 is 0 or 1, respectively).
In summary, arithmetic operators in C++ are used to perform mathematical operations on numerical values. The addition, subtraction, multiplication, division, and modulus operators are used to perform addition, subtraction, multiplication, division, and remainder calculations, respectively.