Write a Java Program to Check if two of three boolean variables are true

As a Java programmer, it is important to have a good understanding of boolean variables and how to manipulate them.

In this tutorial, we will discuss how to check if two out of three boolean variables are true using a Java program.


Boolean variables are used to store true or false values.

They are often used in conditional statements to determine the flow of a program. In Java, boolean variables are declared using the boolean keyword.

To check if two out of three boolean variables are true, we can use a combination of logical operators such as the AND operator (&&) and the OR operator (||).

Here is an example Java program that demonstrates how to check if two out of three boolean variables are true:

public class BooleanVariables {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        boolean c = true;
        
        if ((a && b) || (a && c) || (b && c)) {
            System.out.println("Two of the three boolean variables are true");
        } else {
            System.out.println("Two of the three boolean variables are not true");
        }
    }
}

In this example, we have declared three boolean variables: a, b, and c.

We have initialized them with true and false values.

Next, we have used an if-else statement to check if two out of three boolean variables are true.

We have used the logical operators && and || to combine the boolean expressions.

The first expression (a && b) checks if both a and b are true.

The second expression (a && c) checks if both a and c are true.

The third expression (b && c) checks if both b and c are true.

If any one of these expressions evaluates to true, then the program will print “Two of the three boolean variables are true”.

Otherwise, it will print “Two of the three boolean variables are not true”.


In conclusion, checking if two out of three boolean variables are true can be done using logical operators such as && and ||.

By using if-else statements, we can determine if the expression evaluates to true or false and take appropriate action in our program.