Java program to convert decimal to binary. Here is the following code that you are searching for it. The following program has been written in three different ways using arrays, using the static method, using recursion, and vice versa conversion from Binary to decimal.
- What is binary and how it represents?
A binary number is a number expressed in the base 2 numeral system. A Binary Number is made up of only 0s and 1s.
Example of a binary number is :
101010
- What is Decimal?
A decimal number system is a term with base 10. It’s most commonly used number system. The decimal number system consists of 10 digits from 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
1. Java Code Using Arrays
Here is the program using arrays with sample outputs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.*; class DectoBin { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter a decimal number"); int n=sc.nextInt(); int bin[]=new int[100]; int i = 0; while(n > 0) { bin[i++] = n%2; n = n/2; } System.out.print("Binary number is : "); for(int j = i-1;j >= 0;j--) { System.out.print(bin[j]); } } } |
1 2 3 |
Enter a decimal number 4 Binary number is : 100 |
2. Using static Method
- There you go another program using the static method to convert decimal to binary.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.*; class DtoB { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter a decimal number"); int n=sc.nextInt(); System.out.print("Binary number is : "); binary(n); } static void binary(int num) { int i = 0; int bin[]=new int[100]; bin[0]=0; while(num>0) { bin[i++] = num%2; num = num/2; } for(int j =i-1;j >= 0;j--) { System.out.print(bin[j]); } } } |
1 2 3 |
Enter a decimal number 100 Binary number is : 1100100 |
3. Using Recursion
Another sample program using recursion method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.*; class DecimaltoBinary { int bin[]=new int[100],i = 0; void binary(int num) { if(num>0) { bin[i++] = num%2; num = num/2; binary(num); } for(i=i-1;i >= 0;i--) { System.out.print(bin[i]); } } public static void main(String arg[]) { DecimaltoBinary d=new DecimaltoBinary(); Scanner sc=new Scanner(System.in); System.out.println("Enter a decimal number"); int n=sc.nextInt(); System.out.print("Binary number is : "); d.binary(n); } } |
Output:
1 2 3 |
Enter a decimal number 54 Binary number is : 110110 |
4. Using Stack
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.*; class DtoB { public static void main(String[] arg) { Scanner sc= new Scanner(System.in); Stack<Integer> s= new Stack<Integer>(); // Create Stack object System.out.println("Enter decimal number: "); int n = sc.nextInt(); while(n != 0) { int d = n % 2; s.push(d); n /= 2; } System.out.print("Binary representation is : "); while (!(s.isEmpty() )) { System.out.print(s.pop()); } } } |
1 2 3 |
Enter decimal number: 16 Binary representation is : 10000 |