conversion of decimal to binary in java code example
Example 1: int to binary java
String binary = Integer.toBinaryString(num);
Example 2: Convert decimal number to binary in java without using array
public class DecimalToBinaryWithoutArray
{
static void toBinary(int num)
{
StringBuilder sb = new StringBuilder();
int a = 0;
while(num > 0)
{
sb.append(num % 2);
a++;
num = num / 2;
}
System.out.println(sb.reverse());
}
public static void main(String[] args)
{
int number = 20;
toBinary(number);
}
}