-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
43 lines (36 loc) · 1.09 KB
/
Main.java
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
package com.yurii.salimov.lesson06.task06;
import com.yurii.salimov.lesson06.task03.ArraysToString;
import com.yurii.salimov.lesson06.task03.ArraysToStringImpl;
import java.util.Random;
import java.util.Scanner;
/**
* 6.6 Задача
*
* Ввести с консоли целое число. Посчитать количество
* единиц в его бинарном представлении (“1011” –> 3).
*
* @author Yuriy Salimov ([email protected])
*/
public class Main {
public static void main(String[] args) {
final int number = readNumber();
final int count = oneCount(number);
System.out.println(count);
}
private static int oneCount(int number) {
int count = 0;
while (number > 0) {
if (number % 2 != 0) {
count++;
}
number /= 2;
}
return count;
}
private static int readNumber() {
final Scanner scanner = new Scanner(System.in);
final int number = scanner.nextInt();
scanner.close();
return number;
}
}