Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Shavit Borisov
- // CW
- import java.util.Scanner;
- public class BinToDec {
- public static void main(String[] args)
- {
- Scanner in = new Scanner (System.in);
- System.out.println("Enter a 4 digit Binary number:");
- int bin = in.nextInt();
- int first = bin % 10;
- int second = bin / 10 % 10;
- int third = bin / 100 % 10;
- int fourth = bin / 1000 % 10;
- double Dec = first * Math.pow(2,0) + second * Math.pow(2, 1) + third * Math.pow(2, 2) + fourth * Math.pow(2, 3);
- System.out.printf("The Decimal value of your number is %.0f.", Dec);
- in.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment