- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Arithmetic Operators in C++
C++ has 5 basic arithematic operators. They are −
- Addition(+)
- Subtraction(-)
- Division(/)
- Multiplication(*)
- Modulo(%)
Example
These operators can operate on any arithmetic operations in C++. Lets have a look at an example −
#include <iostream> using namespace std; main() { int a = 21; int b = 10; int c ; c = a + b; cout << "Line 1 - Value of c is :" << c << endl ; c = a - b; cout << "Line 2 - Value of c is :" << c << endl; c = a * b; cout << "Line 3 - Value of c is :" << c << endl; c = a / b; cout << "Line 4 - Value of c is :" << c << endl; c = a % b; cout << "Line 5 - Value of c is :" << c << endl; return 0; }
Output
This will give the output −
Line 1 - Value of c is :31 Line 2 - Value of c is :11 Line 3 - Value of c is :210 Line 4 - Value of c is :2 Line 5 - Value of c is :1
There are some compound arithmetic operators also available in c++. The compound arithmetic assignment operators are specified in the form e1 op= e2, where e1 is a modifiable l-value not of const type and e2 is one of the following:
- An arithmetic type
- A pointer, if op is + or –
The e1 op= e2 form behaves as e1 = e1 op e2, but e1 is evaluated only once.
The following are the compound arithmatic assignment operators in C++ −
Operators | Description |
---|---|
*= | Multiply the value of the first operand by the value of the second operand; store the result in the object specified by the first operand. |
/= | Multiply the value of the first operand by the value of the second operand; store the result in the object specified by the first operand. |
/= | Divide the value of the first operand by the value of the second operand; store the result in the object specified by the first operand. |
%= | Take modulus of the first operand specified by the value of the second operand; store the result in the object specified by the first operand. |
+= | Add the value of the second operand to the value of the first operand; store the result in the object specified by the first operand. |
–= | Subtract the value of the second operand from the value of the first operand; store the result in the object specified by the first operand. |
Let's have a look at an example using some of these operators −
#include<iostream> using namespace std; int main() { int a = 3, b = 2; a += b; cout << a << endl; a -= b; cout << a << endl; a *= b; cout << a << endl; a /= b; cout << a << endl; return 0; }
This will give the output −
5 3 6 3
Note that Compound assignment to an enumerated type generates an error message. If the left operand is of a pointer type, the right operand must be of a pointer type or it must be a constant expression that evaluates to 0. If the left operand is of an integral type, the right operand must not be of a pointer type.