-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day10-Binary-Numbers
51 lines (40 loc) · 1.16 KB
/
Day10-Binary-Numbers
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Problem:
Objective
Today, we're working with binary numbers. Check out the Tutorial tab for learning materials and an instructional video!
Task
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.
Solution:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int n = scanner.nextInt();
int count=0;
int consecutives =0;
while(n>0)
{
if(n%2==1)
{
count++;
if(count>=consecutives)
{
consecutives=count;
}
}
else
{
count=0;
}
n=n/2;
}
scanner.close();
System.out.println(consecutives);
}
}