forked from int28h/JavaTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_Binary_Numbers.java
More file actions
34 lines (30 loc) · 864 Bytes
/
10_Binary_Numbers.java
File metadata and controls
34 lines (30 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* 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.
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String binaryNumber = Integer.toBinaryString(n);
char [] data = binaryNumber.toCharArray();
int result = 0, temp = 0;
for(char ch : data) {
if (ch == '1') {
temp++;
} else {
temp = 0;
}
if (temp > result) {
result = temp;
}
}
System.out.println(result);
in.close();
}
}