Java Program to Check if Two of Three Boolean Variables are True Last Updated : 27 Nov, 2020 Summarize Comments Improve Suggest changes Share Like Article Like 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: using an if-else.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!"); } } OutputTwo 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!"); } } OutputTwo 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!"); } } OutputTwo flags are not true!Time Complexity: O(1)Space Complexity: O(1) Comment More infoAdvertise with us Next Article Java Program to convert integer to boolean M mallikagupta90 Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads 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 ar 3 min read Java Program to Convert String to boolean In Java, to convert a string to a Boolean, we can use Boolean.parseBoolean(string) to convert a string to a primitive Boolean, or Boolean.valueOf(string) to convert it to a Boolean object. The Boolean data type only holds two possible values which are true and false. If the string equals "true" (ign 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 Convers 4 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, 2 min read Java Program to Find a triplet that sum to a given value Given an array and a value, find if there is a triplet in array whose sum is equal to the given value. If there is such a triplet present in array, then print the triplet and return true. Else return false. Examples:Â Â Input: array = {12, 3, 4, 1, 6, 9}, sum = 24;Â Output: 12, 3, 9Â Explanation: There 7 min read Check if Two Integers are Equal or Not in Java Checking two integers equal or not in Java is done by various approaches. Arithmetic operatorComparison OperatorsString functionsXOR operatorComplement (~) and bit-wise (&) operator Example Input: FirstNumber = 15 SecondNumber= 15 Output: Numbers are same Input: FirstNumber = 15 SecondNumber= 25 2 min read Like