Arithmetic and Relational Operators in C++ tutorial

9/12/2025

Arithmetic Relational Operars in C++ cplus

Go Back

Arithmetic and Relational Operators in C++

Introduction

Operators are symbols that tell the compiler to perform specific operations on variables and values. Arithmetic operators handle mathematical operations, while relational operators are used to compare values.


Arithmetic  Relational Operars in C++ cplus

Arithmetic Operators

These are used to perform basic mathematical operations.

OperatorDescriptionExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division6 / 32
%Modulus (remainder)5 % 32

Example:

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 3;
    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;
    return 0;
}

Relational Operators

These are used to compare two values and return a boolean (true or false).

OperatorDescriptionExampleResult
==Equal to5 == 3false
!=Not equal to5 != 3true
>Greater than5 > 3true
<Less than5 < 3false
>=Greater than or equal to5 >= 3true
<=Less than or equal to5 <= 3false

Example:

#include <iostream>
using namespace std;

int main() {
    int x = 5, y = 3;
    cout << (x == y) << endl;  // 0
    cout << (x != y) << endl;  // 1
    cout << (x > y) << endl;   // 1
    cout << (x < y) << endl;   // 0
    cout << (x >= y) << endl;  // 1
    cout << (x <= y) << endl;  // 0
    return 0;
}

Output:

0
1
1
0
1
0

Summary

  • Arithmetic operators perform basic mathematical operations.

  • Relational operators compare values and return a boolean result.

  • They are crucial for building expressions, conditions, and control structures in C++.

Table of content