# Java Example program convert Decimal to Binary

• On of the way to convert decimal number to its binary representation using the Integer.toBinaryString() method. This method takes an integer as an argument and returns its binary representation as a string. Here is an example:
public static String decimalToBinary(int decimal) {
return Integer.toBinaryString(decimal);
}

We can call this method and pass a decimal number as an argument, and it will return the binary representation of that number. For example:

int decimal = 10;
String binary = decimalToBinary(decimal);
System.out.println(binary); // Output: 1010

In the above method will return a string representation of the binary number, if we want to have it as an integer you can use Integer.parseInt(binary, 2) to convert it to an int.

Another way to convert a decimal number to its binary representation is by using bit shifting and bitwise operations. You can repeatedly divide the decimal number by 2 and keep track of the remainder. The remainder represents the least significant bit of the binary representation. You can then reverse the order of the remainders to get the binary representation of the decimal number.

In summary, In Java you can use the Integer.toBinaryString() method to convert a decimal number to its binary representation as a string, and you can use Integer.parseInt(binary, 2) to convert it to an int. Also, you can use bit shifting and bitwise operations to convert a decimal number to its binary representation.

Another way to convert a decimal number to its binary representation is by using bit shifting and bitwise operations.
This method involves repeatedly dividing the decimal number by 2 and keeping track of the remainders. The remainders represent the least significant bits of the binary representation.

public static String decimalToBinaryUsingBitOperation(int decimal) {
StringBuilder binary = new StringBuilder();
while (decimal > 0) {
binary.append(decimal % 2);
decimal = decimal >> 1;
}
return binary.reverse().toString();
}

the function uses a while loop to divide the decimal number by 2 and keep track of the remainders. The remainders are appended to a StringBuilder object. The while loop continues until the decimal number is 0. At the end of the loop, the binary representation of the decimal number is obtained by reversing the order of the remainders using the reverse() method of the StringBuilder. The binary representation is then returned as a string.

This method will also work for negative decimal numbers, but the result will be the two's complement representation of the negative number, it will have a leading 1 in the most significant bit.

It's worth noting that, the above method will return the binary representation as a string, if you want to have it as an integer you can use Integer.parseInt(binary, 2) to convert it to an int.

In summary, there are different ways to convert a decimal number to its binary representation in Java. One way is to use the Integer.toBinaryString() method, another way is by using bit shifting and bitwise operations, this method uses a while loop to repeatedly divide the decimal number by 2 and keep track of the remainders, which represent the binary representation of the decimal number. The result can be returned as a string or as an integer by using the Integer.parseInt(binary, 2)

• We can convert binary to decimal in three ways
1.Using Integer.toBinaryString(int num);
2.Using Stack
3.Using Custom logic

1.Write a Java Program to convert decimal to binary in java using Integer.toBinaryString(int num)

1. import java.util.Scanner;
2. public class ConvertDecimalToBinary{
3.
4.     /**
5.      *www.instanceofjava.com
6.      */
7.
8.  public static void main(String[] args) {
9.
10.  System.out.println("\nBinary representation of 1: ");
11.  System.out.println(Integer.toBinaryString(1));
12.  System.out.println("\nBinary representation of 4: ");
13.  System.out.println(Integer.toBinaryString(4));
14.  System.out.println("Binary representation of 10: ");
15.  System.out.println(Integer.toBinaryString(10));
16.  System.out.println("\nBinary representation of 12: ");
17.  System.out.println(Integer.toBinaryString(12));
18.  System.out.println("\nBinary representation of 120: ");
19.  System.out.println(Integer.toBinaryString(120));
20.  System.out.println("\nBinary representation of 500: ");
21.  System.out.println(Integer.toBinaryString(500));

22. }
23.
24. }

Output:

1. Binary representation of 1:
2. 1
3. Binary representation of 4:
4. 100
5. Binary representation of 10:
6. 1010
7. Binary representation of 12:
8. 1100
9. Binary representation of 120:
10. 1111000
11. Binary representation of 500:
12. 111110100

2.Write a Java Program to convert decimal to binary in java

1. import java.util.Scanner;
2. import java.util.Stack;
3. public class ConvertDecimalToBinary{
4.
5.     /**
6.      *www.instanceofjava.com
7.      */
8.
9.
10.  public static void main(String[] args) {
11.
12. Scanner in = new Scanner(System.in);
13.
14. // Create Stack object
15. Stack<Integer> stack = new Stack<Integer>();
16.
17. //Take  User input from keyboard
18.  System.out.println("Enter decimal number: ");
19.  int num = in.nextInt();
20.
21. while (num != 0)
22. {
23.    int d = num % 2;
24.    stack.push(d);
25.    num /= 2;
26.
27.  }
28.
29.  System.out.print("\nBinary representation is:");
30.
31. while (!(stack.isEmpty() ))
32.  {
33.    System.out.print(stack.pop());
34.  }
35.
36.
37. System.out.println();
38.
39. }
40.
41. }
42.
43. }

Output:

1. Enter decimal number:
2. 12
3.
4. Binary representation is:1100

3.Write a Java Program to convert decimal to binary in java

1. import java.util.Scanner;
2. public class ConvertDecimalToBinary{
3.
4.     /**
5.      *www.instanceofjava.com
6.      */
7.
8. public static void convertDeciamlToBinary(int num){
9.
10.   int binary[] = new int[40];
11.          int index = 0;
12.  while(num > 0){
13.            binary[index++] = num%2;
14.            num = num/2;
15.  }

16. for(int i = index-1;i >= 0;i--){
17.            System.out.print(binary[i]);
18. }
19.
20. }
21.
22.  public static void main(String[] args) {
23.
24. System.out.println("Binary representation of 1: ");
25. convertDeciamlToBinary(1);
26.
27. System.out.println("\nBinary representation of 4: ");
28. convertDeciamlToBinary(4);
29.
30. System.out.println("\nBinary representation of 10: ");
31. convertDeciamlToBinary(10);
32.
33. System.out.println("\nBinary representation of 12: ");
34.  convertDeciamlToBinary(12);
35.
36. }
37.
38. }

Output:

1. Binary representation of 1:
2. 1
3. Binary representation of 4:
4. 100
5. Binary representation of 10:
6. 1010
7. Binary representation of 12:
8. 1100

You Might Like:

Java programming interview questions