Java Program to Check if Two of Three Boolean Variables are True Last Updated : 27 Nov, 2020 Comments Improve Suggest changes 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 Check if Two of Three Boolean Variables are True 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 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 inte 2 min read Java Program to Find the Largest of three Numbers Problem Statement: Given three numbers x, y, and z of which aim is to get the largest among these three numbers.Example:Â Input: x = 7, y = 20, z = 56Output: 56 // value stored in variable zFlowchart For Largest of 3 numbers:Algorithm to find the largest of three numbers:1. Start2. Read the three num 4 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 Check if Particular Value Exists in Java HashMap Java HashMap is an implementation of the Map interface which maps a Value to a Key which essentially forms an associative pair wherein we can call a Value based on the Key. Java HashMap provides a lot of advantages such as allowing different data types for the Key and Value which makes this data str 5 min read Check if Particular Key Exists in Java HashMap HashMap in Java is the realization of the Hash Table data structure of sorts. It is composed of Key and Value pairs which are symbolically represented as <K,V> where K stands for Key and V for Value. It is an implementation of the Maps Interface and is advantageous in the ways that it provides 5 min read Like