dec2binary.java 785 B

12345678910111213141516171819202122232425262728293031
  1. import flanagan.io.KeyboardInput;
  2. import java.math.BigInteger;
  3. import java.lang.Math;
  4. public class dec {
  5. public static void main(String arg[]) {
  6. KeyboardInput kb = new KeyboardInput();
  7. int decimalNumber = kb.readInt("decimal number =");
  8. int currentNumber, sum = 0, cur = 0; i = 0;
  9. while (true) {
  10. if (decimalNumber - Math.pow(2,i) < 0) {
  11. break;
  12. }
  13. i++;
  14. }
  15. String binary = "";
  16. for (int j = i - 1; j >= 0; j--) {
  17. if (decimalNumber - Math.pow(2,j) < 0) {
  18. binary += "0";
  19. } else {
  20. binary += "1";
  21. decimalNumber -= Math.pow(2,j);
  22. }
  23. }
  24. System.out.println(binary);
  25. }
  26. }