Sunday, 24 May 2015

Convert decimal to binary using Java..........

here is the code ......


       
import java.io.*;
import java.util.*;
import java.math.*;
class decimal_to_binary {


 public static void main(String args[]) {
  int k = 10, i = 1;

  while ((2 << i) <= k) {
   i++;
  }
  i++;

  int[] a = new int[i];

  for (int n = i - 1; n >= 0; n--) {
   a[n] = k % 2;
   k = k / 2;
  }

  for (int g: a)
   System.out.print(g);

 }

}

       
 

No comments:

Post a Comment