Swift Program to Check if two of three boolean variables are true


This tutorial will discuss how to write a Swift program to check if two of three boolean variables are true.

Given three boolean values now we have to check if two of the boolean variables are true or not. Boolean variables are those variables that either contain true or false. We can also use this program to check, out of given three conditions two are true or not.

Below is a demonstration of the same −

Suppose we enter the following input −

Value1 = true
Value2 = true
Value3 = false

Following is the desired output −

Two of the three variables are true

Algorithm

Following is The algorithm to check if two of three boolean variables are true −

  • Step 1 − Create three variables with predefined boolean values or with user-defined boolean values named as “num1”, “num2”, and “num3”.

  • Step 2 − Create another bool type variable that stores the final result named “res1”.

  • Step 4 − Using if-else statement, compare the three values with each other using || and && operators and store the result in the “res1” variable.

  • Step 5 − If the two variables are true out of three variable, then display "Two of the three variables are true”. Otherwise display “Two of the three variables are not true”.

Example 1

The following program shows how to check if two of three boolean variables are true.

import Foundation import Glibc var num1 = true var num2 = true var num3 = false var res1 : Bool if (num1){ // If num1 is true and either num2 or num3 is true // then res1 is true res1 = num2 || num3 } else { // If num1 is false and both // num2 and num3 is true then the // res1 is also true res1 = num2 && num3 } if (res1){ print("Two of the three variables are true") } else { print("Two of the three variables are not true") }

Output

Two of the three variables are true

Here in the above code, we create three bool type variable with values named as “num1 = true”, “num2 = true”, and “num3 = false”. Now we use if statement to compare the three variables with each and store the result in the “res1” variable as shown in the below code −

if (num1) {
   res1 = num2 || num3
} else {
   res1 = num2 && num3
}

Here, we will check if num1 is true, then the res1 should be true if either num2 or num3 is true. Otherwise, if num1 is false, then res1 will be true when both num2 and num3 are true. Here, num1 and num2 both are true so the output will be "Two of the three variables are true”.

Example 2

The following program shows how to check if two of three boolean variables are true.

import Foundation import Glibc print("Enter the value of variables either true or false") print("Variable 1-") // Reading the input from the user and convert them into boolean type var num1 = Bool(readLine()!)! print(num1) print("Variable 2-") var num2 = Bool(readLine()!)! print(num2) print("Variable 3-") var num3 = Bool(readLine()!)! print(num3) var res1 : Bool if (num1){ res1 = num2 || num3 } else { res1 = num2 && num3 } if (res1) { print("Two of the three variables are true") } else { print("Two of the three variables are not true") }

STDIN Input

Enter the value of variables either true or false
Variable 1-
true
Variable 2-
false
Variable 3-
true

Output

Two of the three variables are true

Here in the above code, we take the value of three variables from the user with the help readLine() function. readLine() function takes values in string so we use Bool() function to convert the input value in boolean type. Now we compare the variables with each other to find out if the two of the variables are true or not. So the output is "Two of the three variables are true".

Updated on: 05-Aug-2022

343 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements