Skip to content

Instantly share code, notes, and snippets.

@kryvoboker
Last active May 19, 2020 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kryvoboker/d0686dc2dbf6d14f049a9e6df2e6eee6 to your computer and use it in GitHub Desktop.
Save kryvoboker/d0686dc2dbf6d14f049a9e6df2e6eee6 to your computer and use it in GitHub Desktop.
HomeWork7
import java.util.Scanner;
/**
*
* @author kamaz
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a;
System.out.println("Input numbers");
a = sc.nextLine();
System.out.println("The number is: " + getNumber(a));
}
public static int getNumber(String a) {
int n = 0;
char[] numbers = a.toCharArray();
for (int i = numbers.length - 1; i >= 0; i--) {
if (numbers[i] == '1') {
n += (int) Math.pow(2, numbers.length - 1 - i);
}
else {
}
}
return n;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment