import java.util.*;
public class Binary
{
// instance variables - replace the example below with your own
public Binary()
{
// initialise instance variables
Scanner scan = new Scanner(System.in);
System.out.println("Input your binary code with no spaces");
String code = scan.next();
int arr[] = new int[code.length()];
int c = 0;
for (int i = 1; i <= code.length(); i++){
String part = code.substring(c,i);
int p = Integer.parseInt(part);
arr[c] = p;
c++;
}
//int[] arr = {0, 1, 0, 1, 1, 1, 1, 1};
System.out.println(calc(arr));
}
public int calc(int[] x){
int x2 = 0;
int b = 1;
for(int i = x.length - 1; i > 0; i--){
if (x[i] == 1){
x2 = x2 + b;
}
b = b * 2;
}
return x2;
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.