-
-
Save alexfernandez803/48488bca6a8c7b3f18d1ca41cecaeeb2 to your computer and use it in GitHub Desktop.
Given a base- 10 integer, n , convert it to binary (base-2). Then find and print the base- 10 integer denoting the maximum number of consecutive 1's in n's binary representation.
This file contains hidden or 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; | |
| public class Binary | |
| { | |
| public static String convertToBinary(int decnumber) | |
| { | |
| String binaryString = " "; | |
| if (decnumber > 0) | |
| { | |
| binaryString = Integer.toBinaryString(decnumber); | |
| } | |
| return binaryString; | |
| } | |
| public static int findMaxChar(String str) | |
| { | |
| char[] array = str.toCharArray(); | |
| int maxCount = 1; | |
| char maxChar = array[0]; | |
| for(int i = 0, j = 0; i < str.length() - 1; i = j) | |
| { | |
| int count = 1; | |
| while (++j < str.length() && array[i] == array[j]) | |
| { | |
| count++; | |
| } | |
| if (count > maxCount) | |
| { | |
| maxCount = count; | |
| maxChar = array[i]; | |
| } | |
| } | |
| return (maxCount); | |
| } | |
| public static void main(String [] args) | |
| { | |
| Scanner input = new Scanner(System.in); | |
| int decimalnumber = input.nextInt(); | |
| String result = convertToBinary(decimalnumber); | |
| System.out.println(findMaxChar(result)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment