Shavit

BinToDec

Oct 11th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class BinToDec {
  7.  
  8.     public static void main(String[] args)
  9.    
  10.     {
  11.         Scanner in = new Scanner (System.in);
  12.        
  13.         System.out.println("Enter a 4 digit Binary number:");
  14.             int bin = in.nextInt();
  15.        
  16.         int first = bin % 10;
  17.         int second = bin / 10 % 10;
  18.         int third = bin / 100 % 10;
  19.         int fourth = bin / 1000 % 10;
  20.        
  21.         double Dec = first * Math.pow(2,0) + second * Math.pow(2, 1) + third * Math.pow(2, 2) + fourth * Math.pow(2, 3);
  22.        
  23.         System.out.printf("The Decimal value of your number is %.0f.", Dec);
  24.         in.close();
  25.     }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment