go6eto

binary

Feb 23rd, 2021
652
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class BinaryToDecimal {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         System.out.print("Enter a binary number: ");
  8.         int binaryNum = scan.nextInt();
  9.         int degree = 0;
  10.         int decimalNum = 0;
  11.         while (binaryNum != 0){
  12.             int number = binaryNum % 10;
  13.             decimalNum = (int) (decimalNum + number * Math.pow(2, degree));
  14.             binaryNum = binaryNum / 10;
  15.             degree = degree + 1;
  16.         }
  17.         System.out.println(String.format("Updated number in decimal number system: %d", decimalNum));
  18.     }
  19. }
RAW Paste Data