Given a Boolean value, the task is to convert this Boolean value to an integer value in Java.
Examples:
Input: boolean = true Output: 1 Input: boolean = false Output: 0
Approach:
Below is the implementation of the above approach:
Example 1: When boolean value is true
// Java Program to convert boolean to integer public class GFG { public static void main(String[] args) { // The boolean value boolean boolValue = true; // The expected integer value int intValue; // Check if it's true or false if (boolValue) { intValue = 1; } else { intValue = 0; } // Print the expected integer value System.out.println( boolValue + " after converting into integer = " + intValue); } }
Output:
true after converting into integer = 1
Example 2: When boolean value is false
// Java Program to convert boolean to integer public class GFG { public static void main(String[] args) { // The boolean value boolean boolValue = false; // The expected integer value int intValue; // Check if it's true or false if (boolValue) { intValue = 1; } else { intValue = 0; } // Print the expected integer value System.out.println( boolValue + " after converting into integer = " + intValue); } }
Output:
false after converting into integer = 0
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.