Java Program to check if two of three boolean values are true

basic input/output in c++

What are boolean Values?

A boolean variable is a variable that can hold one of two values: true or false. These values are often used to represent true/false statements or on/off conditions. Boolean variables are commonly used in control statements, such as in an if statement, to determine the flow of a program based on whether a condition is true or false.

In this Article, we will write a program to check if two of three boolean values are true.

boolean Values:

Boolean variables are often defined using the bool data type in many programming languages.
For example, you might use a boolean variable to represent whether a light switch is on or off. The variable could be named light is On and its value could be true if the light is on, or false if the light is off.

Pseudo code For above Example :

if (isLightOn) {
  System.out.println("The light is on.");
} else {
  System.out.println("The light is off.");
}
Output:

The light is on    // if the value is true
The light is off    // if the value is false

This code will print “The light is on.” if the isLightOn variable is true, and “The light is off.” if it is false.

Program to check if two of three boolean values are true

Run
import java.util.*;

public class Main{
  public static void main(String[] args){
    // prints true  
    System.out.println(checkTwoTrue(true, true, true));
    
    // prints true
    System.out.println(checkTwoTrue(true, true, false));  
    
    // prints false
    System.out.println(checkTwoTrue(true, false, false));  
    
    // prints false
    System.out.println(checkTwoTrue(false, false, false));  
  }

  public static boolean checkTwoTrue(boolean b1, boolean b2, boolean b3){
    // check for b1 & b2 for   
    if (b1 && b2) {
      return true;
    }
    
    // check for b1 & b3 for 
    if (b1 && b3) {
      return true;
    }
    
    // check for b2 & b3 for 
    if (b2 && b3) {
      return true;
    }
    
    // false if above 3 if block will not return any value
    return false;
  }
}
Output:

true
true
false
false

This code defines a checkTwoTrue function that takes three boolean values as input and returns true if any of the three possible combinations of two true values is found. If none of the combinations is found, it returns false. The main function tests the checkTwoTrue function with four different input combinations.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription