Java program to convert binary number to decimal value

Java Programming Java8Java Technologies

The java.lang.Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

The parseInt(String str, radix r) method of the Integer class parses the given string argument as a signed integer in the radix specified by the second argument and returns it.

Example

import java.util.Scanner;
public class BinaryToDecimal {
   public static void main(String args[]){
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a binary number ::");
      String binary = sc.next();
      int decimal = Integer.parseInt(binary, 2);
      System.out.println("Decimal value of the given binary number is ::"+decimal);
   }
}

Output

Enter a binary number ::
101001
Decimal value of the given binary number is ::41
raja
Published on 25-Apr-2018 17:02:18
Advertisements