Signup/Sign In

Java Program to use Relational Operators

In this tutorial, we will learn how to perform relational operations. Relational Operators in Java are used for comparing variables for equality, less than, greater than, etc. It always returns a boolean variable. But before moving further, if you are not familiar with the concept of the relational operator in java, then do check the article on Operators in Java.

Input: Enter the first number(num1): 6

Enter the second number(num2): 2

Output:

The result after == is : false

The result after != is : true

The result after num1>num2: true

The result after num1<num2: false

The result after num1>=num2: true

The result after num1<=num2: false

The above problem can be solved in the following ways:

Approach 1: When the values are pre-defined

Approach 2: When the values are user-defined

Let us look at each of these approaches separately.

Program 1: To Perform Relational Operation

In this program, we will perform the relational operations when the values are pre-defined in the program.

Algorithm:

  1. Start
  2. Here, we will use a switch case to choose from different relational operators like ==, !=, <, >, <= and >=.
  3. Declare two variables.
  4. Initialize it.
  5. Perform all the relational operators like ==, !=, <, >, <= and >=.
  6. Display the result of each relational operation.
  7. Stop.

Let us look at the below example for a better understanding.

//Java Program to perform relational operations
public class Main
{
   public static void main(String args[]) 
   {
      //Declare and initialize the variables
      int a = 6;
      System.out.println("The entered value of a is " + a);
      int b = 2;
      System.out.println("The entered value of b is " + b);
      //Perform relational operations
      System.out.println("Values of Relational Operations: ");
      System.out.println("The result of a == b is " + (a == b) );
      System.out.println("The result of a != b is " + (a != b) );
      System.out.println("The result of a > b is " + (a > b) );
      System.out.println("The result of a < b is " + (a < b) );
      System.out.println("The result of b >= a is " + (b >= a) );
      System.out.println("The result of b <= a is " + (b <= a) );
   }
}


The entered value of a is 6
The entered value of b is 2
Values of Relational Operations:
The result of a == b is false
The result of a != b is true
The result of a > b is true
The result of a < b is false
The result of b >= a is false
The result of b <= a is true

Program 2: To Perform Relational Operation

In this program, we will perform the relational operations by taking input from the user.

Algorithm:

  1. Start
  2. Here, we will use a switch case to choose from different relational operators like ==, !=, <, >, <= and >=.
  3. Declare a variable for the same.
  4. Ask the user to initialize it.
  5. Based on the operation chosen, declare two variables.
  6. Ask the user to initialize the variables.
  7. Display the result after performing the relational operations.
  8. Stop.

Let us look at the below example for a better understanding.

//Java program to perform Relational Operators
import java.util.Scanner;
public class Main
{
   public static void main(String args[])
   {   
        Scanner s = new Scanner(System.in);
        while(true)
        {
            System.out.println("");
            
            System.out.println("Choose the operation you want to perform ");
            System.out.println("Choose 1 for  == ");
            System.out.println("Choose 2 for != ");
            System.out.println("Choose 3 for > ");
            System.out.println("Choose 4 for < ");
            System.out.println("Choose 5 for <= ");
            System.out.println("Choose 6 for >= ");
            System.out.println("Choose 7 for EXIT");
            int n = s.nextInt();
            switch(n)
            {
                case 1:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int a = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int b = s.nextInt();
                    System.out.println("The result after == is: "+(a == b));
                    break;
 
                case 2:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int p = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int q = s.nextInt();
                    System.out.println("The result after != is: "+(p != q));
                    break;
 
                case 3:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int x = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int y = s.nextInt();
                    System.out.println("The result after > is : "+(x > y));
                    break;
 
                case 4:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int c = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int d = s.nextInt();
                    System.out.print("The result after < is : "+(c < d));
                    break;
 
                case 5:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int e = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int f = s.nextInt();
                    System.out.println("The result after >= : "+(e >= f));
                    break;
 
                case 6:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int g = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int h = s.nextInt();
                    System.out.println("The result after <= : "+(g <= h));
                    break;
                    
                case 7:
                    System.exit(0);
            }
        }
    }
}


Choose the operation you want to perform
Choose 1 for ==
Choose 2 for !=
Choose 3 for >
Choose 4 for <
Choose 5 for <=
Choose 6 for >=
Choose 7 for EXIT
1
Enter the two numbers to perform operations
Enter the first number : 3
Enter the second number: 2
The result after == is: false

Choose the operation you want to perform
Choose 1 for ==
Choose 2 for !=
Choose 3 for >
Choose 4 for <
Choose 5 for <=
Choose 6 for >=
Choose 7 for EXIT
4
Enter the two numbers to perform operations
Enter the first number : 2
Enter the second number : 7
The result after < is : true
Choose the operation you want to perform
Choose 1 for ==
Choose 2 for !=
Choose 3 for >
Choose 4 for <
Choose 5 for <=
Choose 6 for >=
Choose 7 for EXIT
7



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.