-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHulkCB
49 lines (43 loc) · 1.11 KB
/
HulkCB
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
//https://hack.codingblocks.com/app/practice/1/p/135
#include <iostream>
using namespace std;
long long convertDecimalToBinary(unsigned long long int);
int main ()
{
unsigned long long int n;
// Number of test case
cin >> n;
unsigned long long int arr[n], i;
// Input for each test case
for (i = 1; i <= n; i++)
cin >> arr[i-1];
for (i = 0; i < n; i++)
{
unsigned long long int binaryNumber, sum = 0;
//Function to convert binary to decimal
binaryNumber = convertDecimalToBinary(arr[i]);
while (binaryNumber != 0)
{
unsigned long long int t;
t = binaryNumber%2;
sum = sum + t;
binaryNumber = binaryNumber/10;
}
cout << sum << endl;
}
return 0;
}
//Function to convert Binary to decimal
long long convertDecimalToBinary(unsigned long long int n)
{
long long binaryNumber = 0;
unsigned long long int remainder, i = 1, step = 1;
while (n!=0)
{
remainder = n%2;
n /= 2;
binaryNumber += remainder*i;
i *= 10;
}
return binaryNumber;
}