# Arithmetic Operators In C/C++

Prashant | Sun, 02 Aug, 2020 | 107

## Arithmetic Operators

Arithmetic operators are the special symbols that are used for the Arithmetic / Mathematical operations. These operators can be unary and binary.

Following are the types of Arithmetic Operators,

• Arithmetic unary operators
• Arithmetic binary operators

### Arithmetic unary operators

For the unary operations – we need only one operand. These are the operators,

1. Unary plus (+)
2. Unary minus (-)

Syntax:

```+a
-a
```

Example:

```Input:
int a = -10;

Operation & Output:
+a = -10
-a = 10
```

C++ program to demonstrate the example of arithmetic unary operators

``````#include <iostream>
using namespace std;

int main()
{
int a = 10;
int b = -10;

// printing the values
cout << "a: " << a << endl;
cout << "b: " << b << endl;

// unary plus operations
cout << "+a: " << +a << endl;
cout << "+b: " << +b << endl;

// unary minus operations
cout << "-a: " << -a << endl;
cout << "-b: " << -b << endl;

return 0;
}
``````

Output:

```a: 10
b: -10
+a: 10
+b: -10
-a: -10
-b: 10
```

### Arithmetic binary operators

For the binary operations – we need two operands. These are the operators,

OperatorNameDescription
+Plus operatorReturns the addition of two operands.
-Minus operatorReturns the subtraction of two operands.
*Multiplication operatorReturns the multiplication (product) of two operands.
/Divide operatorReturns the result of the division of two operands i.e. returns the quotient of the division operation.
%Modulus operatorReturns the remainder of the division operation on two operands.

Syntax:

```a + b
a - b
a * b
a / b
a % b
```

Example:

```Input:
int a = 10;
int b = 3;

Operation & Output:
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
```

C++ program to demonstrate the example of arithmetic binary operators

``````#include <iostream>
#include <cmath> // for fmod() func.
using namespace std;

int main()
{
int a = 10;
int b = 3;

// printing the values
cout << "a : " << a << endl;
cout << "b : " << b << endl;

// arithmetic operations
cout << "a + b : " << a + b << endl;
cout << "a - b : " << a - b << endl;
cout << "a * b : " << a * b << endl;
cout << "a / b : " << a / b << endl;
cout << "a % b : " << a % b << endl;
cout << endl;

float x = 10.23f;
float y = 3.10f;

// printing the values
cout << "x : " << x << endl;
cout << "y : " << y << endl;

// arithmetic operations
cout << "x + y : " << x + y << endl;
cout << "x - y : " << x - y << endl;
cout << "x * y : " << x * y << endl;
cout << "x / y : " << x / y << endl;
// % operator doesn't work with float values
// use fmod() for this
// cout << "x % y : " << x % y << endl;
cout << "fmod(" << x << " , " << y << ") : " << fmod(x, y) << endl;

return 0;
}
``````

Output:

```a : 10
b : 3
a + b : 13
a - b : 7
a * b : 30
a / b : 3
a % b : 1

x : 10.23
y : 3.1
x + y : 13.33
x - y : 7.13
x * y : 31.713
x / y : 3.3
fmod(10.23 , 3.1) : 0.93```

0 comments