Convert decimal to binary in Java
In this post, you will learn how to convert a decimal into an equivalent binary number using the Java programming language. Here, we have mentioned two ways to convert a decimal to a binary number.
A decimal number is a base-10 number system where the range of numbers is 0,1,2,3,4,5,6,7,8,9. The main advantages of the decimal number system are easy-to-read, used by humans and easy to manipulate.
A binary number consists of two numbers 0s and 1s. It is expressed in the base-2 numeral system or binary numeral system. As the computer only understands binary language that is 0 or 1, all inputs given to a computer are decoded by it into series of 0's or 1's to process it further.
Java convert decimal to binary using Integer.toBinaryString()
Java provides a built-in Integer.toBinaryString() function to convert various values to a binary string. This function returns a binary value.
public class ConvertDecimalToBinary{
public static void main(String args[]){
System.out.println("Binary value of 10 : "+Integer.toBinaryString(10));
System.out.println("Binary value of 12 : "+Integer.toBinaryString(12));
System.out.println("Binary value of 31 : "+Integer.toBinaryString(31));
System.out.println("Binary value of 41 : "+Integer.toBinaryString(41));
System.out.println("Binary value of 24 : "+Integer.toBinaryString(24));
}
}
Output of the above code:
Binary value of 10 : 1010
Binary value of 12 : 1100
Binary value of 31 : 11111
Binary value of 41 : 101001
Binary value of 24 : 11000
Java convert decimal to binary using loop
Here, we have applied the custom logic using a loop to convert decimal to binary in Java.
import java.io.*;
public class DecimalToBinary
{
// defining function to convert decimal to binary
static void decToBinary(int num)
{
// array to store binary number
int binary[] = new int[1000];
// counter for binary array
int i = 0;
while (num > 0)
{
// storing remainder in binary array
binary[i] = num % 2;
num = num / 2;
i++;
}
// printing binary array in reverse order
for (int j = i - 1; j >= 0; j--)
System.out.print(binary[j]);
}
public static void main (String[] args)
{
System.out.println("Binary representation of 12: ");
decToBinary(12);
System.out.println("\nBinary representation of 31: ");
decToBinary(31);
System.out.println("\nBinary representation of 25: ");
decToBinary(25);
System.out.println("\nBinary representation of 45: ");
decToBinary(45);
}
}
Output of the above code:
Binary representation of 12:
1100
Binary representation of 31:
11111
Binary representation of 25:
11001
Binary representation of 45:
101101
Related Articles
Capitalize first letter of each word JavaConvert binary to decimal in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number