Convert Decimal To Binary in Java
Learn How To Convert Decimal To Binary in Java Programming Language. It is important to know about How A Loop Works to understand this program efficiently.
A Decimal Number or just a Decimal is a number consisting of 0 to 9 digits within it. However, a Binary Number is the one which consists of only 1 and 0. For Computers, Binary Numbers are of importance as they cannot interpret Decimal Numbers.
Therefore, it is important to know How To Convert Decimal Number To Binary in Java Programming Language. Here, we have performed Conversion of Decimal Number To Binary Number using While Loop in Java.
Program To Convert Decimal To Binary in Java Programming
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.io.*; import java.util.*; class DecimalToBinary { public static void main(String args[]) { int z = 1,temp, decimal_number,rem; int binary_number = 0; Scanner sc = new Scanner(System.in); System.out.println("nEnter A Decimal Number:t"); decimal_number = sc.nextInt(); temp = decimal_number; while(decimal_number > 0) { rem = decimal_number%2; decimal_number = decimal_number/2; binary_number = binary_number + (z*rem); z = z*10;lang:default decode:true } System.out.println("nBinary Equivalent of Decimal Number " + temp + " = " + binary_number + "n" ); } } |
Also Read: Java Code To Count Occurrence of Character in String
Output

In case you have any Compilation Errors in Converting Decimal Number To Binary in Java, Mention about it in the Comments Section.