Java Program to Convert Integer Values into Binary
Last Updated :
22 Jun, 2022
Given an integer in Java, your task is to write a Java program to convert this given integer into a binary number.
Example:
Input: = 45
Output: = 101101
Input: = 32
Output: = 100000
Integers: Integers are numbers whose base value is 10. The Integer or int data type is a 32-bit signed two’s complement integer. Its value-range lies between – 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is – 2,147,483,648 and maximum value is 2,147,483,647. Its default value is 0. The int data type is generally used as a default data type for integral values unless there is no problem with memory.
Binary Numbers: A binary number is a number expressed in the base-2 numeral. Binary consists of only 2 digits 0 and 1. Two operators are used in the conversion of integers to binary modulo and division to convert the given input into binary numbers.
Approaches
There are many approaches to convert an integer into binary numbers some of them are discussed here. We will be discussing two of them:
- Using Implementation of Stack
- Using Inbuilt Method- toBinaryString() of the Integer class of Java
1. Using Implementation of Stack
Actually, the binary number consists of only 0 and 1. To convert an integer to binary divide the number by 2 until it becomes 0. In each step take the modulo by 2 and store the remainder into an array or stack. If we store the remainder into an array then print it into reverse order. If we store the remainder into a stack then simply pop one by one element and print it.
Below is the java implementation of the above approach:
Java
// Java Program to Convert Integer Values into Binary
// Importing CLasses/Files
import java.io.*;
public class GFG {
// Function to print binary number
static void printBinary(int[] binary, int id)
{
// Iteration over array
for (int i = id - 1; i >= 0; i--)
System.out.print(binary[i] + "");
}
// Function converting decimal to binary
public static void decimalToBinary(int num)
{
// Creating and assigning binary array size
int[] binary = new int[35];
int id = 0;
// Number should be positive
while (num > 0) {
binary[id++] = num % 2;
num = num / 2;
}
// Print Binary
printBinary(binary, id);
}
// Main Driver Code
public static void main(String[] args)
{
// Entered number to be convert into binary
int num = 45;
// Calling Our Above Function
decimalToBinary(num);
}
}
Time Complexity: O(log2N)
Auxiliary Space: O(log2N)
Using Stack By Creating Object Vector
Java
// Java Program to Convert Integer Values into Binary
// Importing Classes/Files
import java.io.*;
import java.util.Stack;
public class GFG {
// Function to convert integer to binary
static void decimalToBinary(int num)
{
// Creating Stack Object Vector
Stack<Integer> st = new Stack<>();
// Number Should be positive
while (num > 0) {
// Pushing numbers inside stack that
// are divisible by 2
st.push(num % 2);
// Dividing number by 2
num = num / 2;
}
// Checking condition whether stack is empty
while (!(st.isEmpty())) {
// Printing binary number
System.out.print(st.pop());
}
}
// Main driver function
public static void main(String[] args)
{
// Entered number to be converted into binary
int num = 45;
decimalToBinary(num);
}
}
Time Complexity: O(log2N)
Auxiliary Space: O(log2N)
2. Using toBinaryString() inbuilt method of the Integer class of Java
Java
// Java Program to Convert Integer Values into Binary
// Importing Classes/Files
import java.io.*;
class GFG {
// Function converting decimal to binary
static void decimalToBinary(int num)
{
// Function to print integer to binary using
// inbuilt toBinaryString method
System.out.println(Integer.toBinaryString(num));
}
// Main driver function
public static void main(String[] args)
{
// Number to be converted into binary
int num = 45;
// Calling function
decimalToBinary(num);
}
}
Time Complexity: O(log2N)
Auxiliary Space: O(log2N)
Similar Reads
Java Program to Convert Integer List to Integer Array There are many ways to convert integer List to ArrayList where in this article we will be discussing out 2 approaches as below: Using concept od streams in Java8Using Apache Commons LangUsing Guava Library Method 1: Using concept od streams in Java8 So, first, we will get to know about how to conver
3 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 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 Convert Binary to Hexadecimal The Hexadecimal number system as the name suggests comprises 16 entities. These 16 entities consist of 10 digits, 0-9 representing the first 10 numbers of the hexadecimal system as well. For the remaining 6 numbers, we use English alphabets ranging from A through F to represent the numbers 10 to 15.
6 min read
Java Program to Convert Char to Int Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s
4 min read
Java Program For Int to Char Conversion In this article, we will check how to convert an Int to a Char in Java. In Java, char takes 2 bytes (16-bit UTF encoding ), while int takes 4 bytes (32-bit). So, if we want the integer to get converted to a character then we need to typecast because data residing in 4 bytes cannot get into a single
3 min read
Java Program to Convert Binary to Octal A Binary (base 2) number is given, and our task is to convert it into an Octal (base 8) number. There are different ways to convert binary to decimal, which we will discuss in this article.Example of Binary to Octal Conversion:Input : 100100Output: 44Input : 1100001Output : 141A Binary Number System
5 min read
Java Program to Convert Octal to Binary Given an Octal number as input, the task is to convert that number into its Binary equivalent number. Example: Input: Octal Number = 513 Output: Binary equivalent value is: 101001011 Explanation : Binary equivalent value of 5: 101 Binary equivalent value of 1: 001 Binary equivalent value of 3: 011Oc
5 min read
Java Program to Convert Int to Double Java data types can be categorized as primitive and non-primitive. Primitive data types contain a single value, whereas non-primitive data types contain an address of the variable value. Java supports 7 primitive data types - boolean, byte, char, short, int, long, float, and double. These data types
4 min read
Java Program to Convert Binary Code Into Equivalent Gray Code Using Recursion Convert the Binary code of the Number into it's equivalent Gray's code using recursion. Binary is the default way to store numbers, but in many applications, binary numbers are not useful and a variation of binary is needed. Gray code has the property that two successive numbers differ in only one b
3 min read