Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Binary
- {
- public static void main (String[]args){
- int n;
- int counter = 1;
- Scanner scan = new Scanner(System.in);
- System.out.println ("Please a sequence of 1's and 0's");
- n= scan.nextInt();
- for (int index = 0; index < n.length();
- index++) {
- char aChar = n.charAt(index);
- }
- if (n < 0 || n > 1) { //base case
- System.out.println ("Error. Please enter 1 or 0");
- n = scan.nextInt();}
- else if (n < 2){ //base case
- System.out.print(n);
- return;}
- else {
- n = (n/2); //recursive step
- int answer = n%2;
- System.out.print(answer);}
- }
- }
Add Comment
Please, Sign In to add comment