119. How Many Ones Needed?

 avatar
user_6817964
c_cpp
2 years ago
506 B
4
Indexable
int count_ones(int dec_num);

int count_ones(int dec_num) {
	int total = 0;
	while (1) {
		if (dec_num == 1) {
			total++;
			break;
		}
		int temp = dec_num % 2;
		if (temp == 1) {
			total++;
		}
		dec_num /= 2;
	}
	return total;
}

int main() {
	int a, b;

	while (scanf("%d%d", &a, &b)) {
		int total_ones_num = 0;
		if (a == 0 && b == 0) break;
		for (int i = a; i <= b; i++) {
			total_ones_num += count_ones(i);
		}
		printf("%d\n", total_ones_num);
	}

	return 0;
}
Editor is loading...