ITGUY25 - Editorial

Problem: Contest Page | CodeChef

DIFFICULTY:

EASY.

PROBLEM:

Chef solved so many hard questions, now he wants to solve some easy problems for refreshment. Chef asks Cheffina for the new question. Cheffina challanges the chef to print the total number of 1’s in the binary representation of N(natural number).

Program:

#include<bits/stdc++.h>
using namespace std;
int main()
 {
	//code
	ios_base::sync_with_stdio(false);
   	cin.tie(NULL);
   	cout.tie(NULL);
   	int t;
    cin>>t;
	for(int i=0;i<t;i++)
	{
	    int n,sum=0;
        cin>>n;
	    while(n>0){
	    	if(n%2)sum++;
	    	n=n/2;
	    }
	    cout<<sum<<"\n";
	}
	return 0;
}