1public class DecimalToBinaryWithoutArray
2{
3 static void toBinary(int num)
4 {
5 StringBuilder sb = new StringBuilder();
6 int a = 0;
7 while(num > 0)
8 {
9 sb.append(num % 2);
10 a++;
11 num = num / 2;
12 }
13 System.out.println(sb.reverse());
14 }
15 public static void main(String[] args)
16 {
17 int number = 20;
18 toBinary(number);
19 }
20}
1public class DecimalToBinaryExample2{
2public static void toBinary(int decimal){
3 int binary[] = new int[40];
4 int index = 0;
5 while(decimal > 0){
6 binary[index++] = decimal%2;
7 decimal = decimal/2;
8 }
9 for(int i = index-1;i >= 0;i--){
10 System.out.print(binary[i]);
11 }
12System.out.println();//new line
13}
14public static void main(String args[]){
15System.out.println("Decimal of 10 is: ");
16toBinary(10);
17System.out.println("Decimal of 21 is: ");
18toBinary(21);
19System.out.println("Decimal of 31 is: ");
20toBinary(31);
21}}