fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone {
  9. static void decToBinary(int n) {
  10. System.out.println(Integer.toBinaryString(n));
  11. int[] binaryNum = new int[1000];
  12.  
  13. // counter for binary array
  14. int i = 0;
  15. while (n > 0) {
  16. // storing remainder in binary array
  17. binaryNum[i] = n % 2;
  18. n = n / 2;
  19. i++;
  20. }
  21. ArrayList<Integer> al = new ArrayList<Integer>();
  22.  
  23. // printing binary array in reverse order
  24. for (int j = i - 1; j >= 0; j--) {
  25. if (binaryNum[j] == 0) {
  26. int ctr = 0;
  27. while (binaryNum[j] == 0) {
  28. ctr++;
  29. j--;
  30. }
  31. al.add(ctr);
  32. }
  33. }
  34.  
  35. System.out.println(al);
  36. }
  37.  
  38. // driver program
  39. public static void main(String[] args) {
  40. int n = 1041;
  41. decToBinary(n);
  42. }
  43. }
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
10000010001
[5, 3]