Advertisement
capncoolio

Untitled

Sep 6th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1. package dec2bin;
  2.  
  3. import java.util.*;
  4.  
  5. public class Dec2Bin {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         System.out.print("Enter an int: ");
  9.         int a = sc.nextInt();
  10.         String bin = "";
  11.         while(a > 0) {
  12.             System.out.println(a%2 + "    (" + a + "/2 = " + a/2 + " rem " + a%2 + ")");
  13.             bin += Integer.toString(a%2);
  14.             a /= 2;
  15.         }
  16.         System.out.print("Binary representation: ");
  17.         for(int i = bin.length() - 1; i >= 0; i--) {
  18.             System.out.print(bin.charAt(i));
  19.         }
  20.         System.out.println();
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement