Open In App

Java Program to Check if Two of Three Boolean Variables are True

Last Updated : 27 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Boolean values are true/false.  In this program, we need to check if any two out of three boolean values are true or not.

We will be discussing two major approaches to do it:

  1. using an if-else.
  2. using the ternary operator.

Approach 1: Using if-else condition

We initialize three boolean variable flags, with either a boolean FALSE or a TRUE value.

The counter approach can be used to check if two out of these three boolean variables are TRUE, by incrementing the counter value for each TRUE value. 

Java




// Java program to check if
// two out of three boolean
// variables are true
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        boolean flag1 = true;
        boolean flag2 = true;
        boolean flag3 = false;
  
        // maintaining a counter
        int cnt = 0;
  
        // check if flag1 is true, increment counter
        if (flag1)
            cnt += 1;
  
        // check if flag2 is true, increment counter
        if (flag2)
            cnt += 1;
  
        // check if flag3 is true, increment counter
        if (flag3)
            cnt += 1;
  
        // check counter value
        if (cnt == 2)
  
            System.out.println("Two flags are true!");
  
        else
  
            System.out.println("Two flags are not true!");
    }
}


Output

Two flags are true!
  • Time Complexity: O(1)
  • Space Complexity: O(1)

Another Method: Here, we will check if flag1 is true, then the ans should be true if either of flag2 or flag3 is true. Otherwise, if flag1 is false, then ans will be true when both flag2 and flag3 are true.

Java




// Java program to check 
// if two out of three boolean
// variables are true
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        boolean flag1 = true;
        boolean flag2 = true;
        boolean flag3 = false;
        
        // to store the result
        boolean ans = false;
        
        if (flag1) {
           ans = flag2 || flag3;
        }
        else {
            ans = flag2 && flag3;
        }
        
        if (ans)
            System.out.println("Two flags are true!");
        else
            System.out.println("Two flags are not true!");
    }
}


Output

Two flags are true!
  • Time Complexity: O(1)
  • Space Complexity: O(1)

Approach 3: Using the ternary operator

Java




// Java program to check if
// two out of three boolean
// variables are true
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        boolean flag1 = true;
        boolean flag2 = true;
        boolean flag3 = true;
        
        // to store the result
        boolean ans = false;
        
        ans = flag1 ? flag2 || flag3 : flag2 && flag3;
        
        if (ans)
            System.out.println("Two flags are true!");
        else
            System.out.println("Two flags are not true!");
    }
}


Output

Two flags are not true!
  • Time Complexity: O(1)
  • Space Complexity: O(1)


Similar Reads

Java Guava | Booleans.indexOf(boolean[] array, boolean target) method with Examples
Booleans.indexOf(boolean[] array, boolean target) method of Guava's Booleans Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(boolean[] array, boolean t
3 min read
Java Guava | Booleans.indexOf(boolean[] array, boolean[] target) method with Examples
Booleans.indexOf(boolean[] array, boolean[] target) method of Guava's Booleans Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(boolean[] array, b
2 min read
Java Program to Compare two Boolean Arrays
Two arrays are equal if they contain the same elements in the same order. In java, we can compare two Boolean Arrays in 2 ways: By using Java built-in method that is .equals() method.By using the Naive approach. Examples: Input : A = [true , true , false] A1 = [true, true, false] Output: Both the arrays are equal. Input : A = [true, true, false] A1
3 min read
Java Program to Swap two Variables
Given two numbers x and y, we need to swap their values. In this article, we will learn the Swapping of two numbers in Java. Examples of SwappingInput : x = 10, y = 20;Output : x = 20, y = 10Input : x = 213, y = 109Output : x = 109, y = 213Recommended: Please try your approach on {IDE} first, before moving on to the solution. Steps to Swap Two Numb
4 min read
Java Program to convert boolean to integer
Given a boolean value, the task is to convert this boolean value into an integer value in Java. Examples: Input: boolean = true Output: 1 Input: boolean = false Output: 0 Approach: Get the boolean value to be converted.Check if boolean value is true or falseIf the boolean value is true, set the integer value as 1.Else if the boolean value is false,
2 min read
Java Program to convert integer to boolean
Given a integer value, the task is to convert this integer value into a boolean value in Java. Examples: Input: int = 1 Output: true Input: int = 0 Output: false Approach: Get the boolean value to be converted. Check if boolean value is true or false If the integer value is greater than equal to 1, set the boolean value as true. Else if the integer
2 min read
Java Program to Convert String to Boolean
To convert String to boolean in Java, you can use Boolean.parseBoolean(string). But if you want to convert String to Boolean object then use the method Boolean.valueOf(string) method. Boolean data type consists of only two values i.e true and false. If the string is true (ignoring case), the Boolean equivalent will be true, else false. Tip: In Java
3 min read
Java Program to Convert Boolean to String
In Java programming, you might come across the need to convert a simple "true" or "false" boolean value into a String. It may seem like a challenging task, but fear not! In this article, You will explore some methods to convert a boolean value to a string in Java Method for Boolean to String Conversion in JavaThe method for Conversion from Boolean
4 min read
Java.lang.Boolean Class in Java
Java provides a wrapper class Boolean in java.lang package. The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field, whose type is boolean. In addition, this class provides useful methods like to convert a boolean to a String and a String to a boolean, while dealing with a boolea
8 min read
How to Resolve WEB xml is missing and failOnMissingWebXml is set to true in Eclipse/STS?
Eclipse/STS IDE is generally used to develop Spring applications and what happens is whenever we are creating a simple Maven project and if the web.xml is missing or you have deleted that file then you may encounter this problem inside the pom.xml file corresponding to which do refer to the below image as follows: [caption width="800"] [/caption] N
2 min read
Article Tags :
Practice Tags :