Title description
Enter a positive integer of type int and calculate the number of 1s when the int data is stored in memory.
Enter description:
Enter an integer (int type)
Output description:
After this number is converted to binary, the number of 1 is output
Example 1
Input
5
Output
2
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int num = 0;
while (n) {
if (n & 1) num++;
n = n >> 1;
}
cout << num << endl;
return 0;
}