Shavit

Ex. Bank #19

Dec 6th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.49 KB | None | 0 0
  1. // Shavit Borisov
  2. // HW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class DecToBinary {
  7.  
  8.     public static void main(String[] args)
  9.    
  10.     {
  11.         Scanner in = new Scanner (System.in);
  12.        
  13.         int dec;
  14.        
  15.         System.out.print("Enter a decimal number: ");
  16.         dec = in.nextInt();
  17.        
  18.         System.out.println("The binary equivalent is:");
  19.        
  20.         for(int i = 7; i >= 0; i--)
  21.         {
  22.             if((dec / (int)(Math.pow(2, i))) % 2 == 1)
  23.                 System.out.print("1");
  24.             else
  25.                 System.out.print("0");
  26.         }
  27.        
  28.         in.close();
  29.     }
  30. }
Add Comment
Please, Sign In to add comment