C++ Arithmetic operators

 Relational operators are used to comparing two values and return a result of either true or false. There are six relational operators in C++

  1. Less than (<)
  2. Greater than (>)
  3. Less than or equal to (<=)
  4. Greater than or equal to (>=)
  5. Equal to (==)
  6. Not equal to (!=)
Operator
Description
<
Less than
<
Greater than
<=
>=
Greater than or equal to
==
Equal to
!=
Not equal to

Here is an example program that uses relational operators:

#include <iostream>

using namespace std;

int main() {
    int num1 = 10, num2 = 20;

    // Using relational operators
    if (num1 == num2) {
        cout << "num1 is equal to num2" << endl;
    } else if (num1 != num2) {
        cout << "num1 is not equal to num2" << endl;
    } else if (num1 < num2) {
        cout << "num1 is less than num2" << endl;
    } else if (num1 > num2) {
        cout << "num1 is greater than num2" << endl;
    } else if (num1 <= num2) {
        cout << "num1 is less than or equal to num2" << endl;
    } else if (num1 >= num2) {
        cout << "num1 is greater than or equal to num2" << endl;
    }

    return 0;
}

Output:

num1 is not equal to num2

In this example, we define and initialise the two integer variables num1 and num2 to 10 and 20, respectively. The values of num1 and num2 are then compared using the six relational operators, and based on the comparison’s outcome, the relevant message is printed out.

The message “num1 is not equal to num2” is printed because the num1!= num2 phrase evaluates to true in this instance.