fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5. static String common_ans(int line) {
  6.  
  7. if (line == 0) {
  8. return "";
  9. }
  10.  
  11. String num = "";
  12. for (int i = line; i > 0; i /= 2) {
  13. int remainder = i % 2;
  14. num = remainder + num;
  15. }
  16.  
  17. return num;
  18. }
  19.  
  20. static String library_ans(String line) {
  21. String num = Integer.toBinaryString(Integer.parseInt(line, 10));
  22. return num;
  23. }
  24.  
  25. public static void main(String[] args) {
  26. Scanner sc = new Scanner(System.in);
  27. System.out.println("If the input 3 were decimal, its binary would be " + common_ans(sc.nextInt()) + ".");
  28. sc.nextLine();
  29. System.out.println("If the input 6 were decimal, its binary would be " + library_ans(sc.nextLine()) + ".");
  30. }
  31.  
  32. }
Success #stdin #stdout 0.19s 60952KB
stdin
3
6
stdout
If the input 3 were decimal, its binary would be 11.
If the input 6 were decimal, its binary would be 110.