C++ Program to Implement Relational operators in C++.


January 22, 2023, Learn eTutorial
1457

Here, the program discusses the relational operators in C++. These relational operators are used in decision-making and loops.

What are Relational operators in C++?

Relational operators can be defined as the operators which are used to define a relation between some values. These include numerical equality (e.g., 7 = 7) and inequalities (e.g., 4 ≥ 3). There are different types of relational operators and we use them in the decision-making process.

C++ program to implement the various relational operators in C++

Declare two integer type variables a and b and assign two values to the variables. Declare another Boolean-type variable result to store the result. Check whether the value entered for a is equal to the value entered for b by using the operator ‘ ==’. If true then return a value of 1 otherwise return a value of 0.

Check whether the value entered for a is not equal to the value entered for b by using the operator ‘ !=’. If true then return a value of 1 otherwise return a value of 0.

Check whether the value entered for a is greater than the value entered for b by using the operator ‘ >’. If true then return a value of 1 otherwise return a value of 0.

Check whether the value entered for a is less than the value entered for b by using the operator ‘ <’. If true then return a value of 1 otherwise return a value 0.

Check whether the value entered for a is greater than or equal to the value entered for b by using the operator ‘ >=’. If true then return a value 1 otherwise return a value 0.

Check whether the value entered to a is less than or equal to the value entered for b by using the operator ‘ <=’. If true then return a value 1 otherwise return a value 0.

Display the result on the screen

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main();

Step 4: Declare two integer variables a and b and assign two values; int a = 3; int b = 5;

Step 5: Declare a variable result to store the status of the result;

Step 6: Check for a == b; return 1 for true and 0 for false;

Step 7: Check for a != b; return 1 for true and 0 for false;

Step 8: Check for a > b; return 1 for true and 0 for false;

Step 9: Check for a < b; return 1 for true and 0 for false;

Step 10: Check for a >= b; return 1 for true and 0 for false;

Step 11: Check for a >= b; return 1 for true and 0 for false;

Step 12: Print the status of the variable result for every operators;

Step 13: Exit;
 

C++ Source Code

                                          #include <iostream>
using namespace std;

int main() {
    int a, b;
    a = 3;
    b = 5;
    bool result;

    result = (a == b);   // false
    cout << "3 == 5 is " << result << endl;

    result = (a != b);  // true
    cout << "3 != 5 is " << result << endl;

    result = a > b;   // false
    cout << "3 > 5 is " << result << endl;

    result = a < b;   // true
    cout << "3 < 5 is " << result << endl;

    result = a >= b;  // false
    cout << "3 >= 5 is " << result << endl;

    result = a <= b;  // true
    cout << "3 <= 5 is " << result << endl;

    return 0;
}
                                      

OUTPUT

3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1
3 >= 5 is 0
3 <= 5 is 1