Open In App

Java Program to convert boolean to integer

Last Updated : 25 Apr, 2023
Comments
Improve
Suggest changes
1 Like
Like
Report

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 false
  • If the boolean value is true, set the integer value as 1.
  • Else if the boolean value is false, set the integer value as 0.

Below is the implementation of the above approach: Example 1: When boolean value is true 

Output:
true after converting into integer = 1

The time complexity is O(1), which means it will take a constant amount of time to execute, regardless of the input size.

The auxiliary space used by the program is also constant.

Example 2: When boolean value is false 

Output:
false after converting into integer = 0

Next Article
Article Tags :
Practice Tags :

Similar Reads