Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Friday 30 November 2018

Binary to Decimal in java

/*
input = 10111
output = 23

*/


import java.util.Scanner;

class GFG {
public static void main (String[] args) {
//System.out.println("GfG!");
Scanner scan = new Scanner(System.in);
String binaryNumber = scan.next();
String srr[] = binaryNumber.split("");
int arr[] = new int[srr.length];
int len = arr.length-1;
for(int i=0;i<arr.length;i++){
    arr[len--] = Integer.parseInt(srr[i]);
}
int sum = 0;
for(int i = 0; i< arr.length  ; i++){
    sum += arr[i]*Math.pow(2,i);
}
System.out.println(sum);
}
}
x

No comments:

Post a Comment