Last active
May 19, 2020 19:35
-
-
Save kryvoboker/d0686dc2dbf6d14f049a9e6df2e6eee6 to your computer and use it in GitHub Desktop.
HomeWork7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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