Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dec2bin;
- import java.util.*;
- public class Dec2Bin {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter an int: ");
- int a = sc.nextInt();
- String bin = "";
- while(a > 0) {
- System.out.println(a%2 + " (" + a + "/2 = " + a/2 + " rem " + a%2 + ")");
- bin += Integer.toString(a%2);
- a /= 2;
- }
- System.out.print("Binary representation: ");
- for(int i = bin.length() - 1; i >= 0; i--) {
- System.out.print(bin.charAt(i));
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement