C++ Arithmetic Operators

Arithmetic operators carry basic arithmetic operations in a C++ program. These operators are used in simple mathematical expressions. They are also known as binary operators because each operator requires at least two operands.

Here is a list of arithmetic operators.

Arithmetic OperatorsDescription
+Addition
Subtraction
*Multiplication
/Division
%Modulo

The operators plus (+), minus (-), and multiplication (*) need two numbers to produce results. Similarly, the divide operator requires two number – a numerator and a denominator. The result of the division is a quotient. The last arithmetic operator is modulo (%) which gives the remainder of a division of two integer numbers (not allowed for float or double types).

If one of the numbers is a floating point number and another integer for any arithmetic operator in C++ will result in a floating point value.

integer + integer = integer
float + integer = float
integer + float = float
float + float = float

For example, the following program is a working example of the above rules.

Program Code:

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{ 
    // variable declaration
    
    int a, b, result1;
    float e,d, result2;
    
    // Variable initialization
    
    a = 20;
    b = 30;
    
    d = 25.5;
    e = 20.3;
    
    result1 = 0;
    result2 = 0.0;

    
    // Arithmetic Expressions
    
    result1 = a + b;
    cout << "a + b =" << result1 << endl;
    
    result2 = a + d;
    cout << "a + d =" << result2 << endl;
    
    result2 = d + a;
    cout << "e + b =" << result2 << endl;
    
    result2 = d + e;
    cout << "d + e =" << result2 << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

a + b = 50

a + d = 45.5

e + b = 45.5

d + e = 45.8

Arithmetic Expressions

The operands and the C++ operators make an expression that evaluates to a single value. An expression can be a simple one with two numbers and a single operator or it can be a complex expression involving many numbers and operators.

From the above example,

e + b  evaluates to 45.5

d + e evaluates to 45.8

But, within the C++ program, an expression is written as

result2 = e + b

result2 = d + e

In a complex expression, the terms are evaluated based on the operator precedence. The expression with higher precedence operator is executed first and then the expression with lower precedence operator.

res = 2 + 4 * ( 1 + 5)

In the above example,

The parentheses ( ) has the highest precedence so ( 1 + 5 ) is evaluated first.

The multiplication * has second highest priority so 4 * ( 1 + 5) is evaluated second.

The plus + is evaluated last.

= 2 + 4 * 6

= 2 + 24

= 26

Example Program: Arithmetic Operators

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    // Variable Declarations
    
    int number1, number2, result1;
    float number3, number4, result2;
    
    // Variable Initialization
    
    number1 = 100;
    number2 = 74;
    result1 = 0;
    
    number3 = 24.65;
    number4 = 12.5;
    result2 = 0.0;
    
    //Arithmetic Expressions
    
    result1 = number1 + number2;
    result2 = number3 + number4;
    cout << "Integer Addition =" << result1 << endl;
    cout << "Float Addition =" << result2 << endl;
    
    result1 = number1 - number2;
    result2 = number3 - number4;
    cout << "Integer Subtraction =" << result1 << endl;
    cout << "Float Subtraction = " << result2 << endl;
    
    result1 = number1 * number2;
    result2 = number3 * number4;
    cout << "Integer Multiplication =" << result1 << endl;
    cout << "Float Multiplication = " << result2 << endl;
    
    result1 = number1 / number2;
    result2 = number3 / number4;
    cout << "Integer Division =" << result1 << endl;
    cout << "Float Division = " << result2 << endl;
    
    result1 = number1 % number2;
    cout << "Integer Modulo Operation =" << result1 << endl;
   
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

The output of the above program is as follows.

Integer Addition =174
Float Addition =37.15
Integer Subtraction =26
Float Subtraction = 12.15
Integer Multiplication =7400
Float Multiplication = 308.125
Integer Division =1
Float Division = 1.972
Integer Modulo Operation =26

Please support us by disabling your adblocker or whitelist this site from your adblocker. Thanks!

turn of adblocker imag