-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path01_von_neuman_loves_binary.cpp
52 lines (41 loc) · 1.14 KB
/
01_von_neuman_loves_binary.cpp
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
48
49
50
51
52
/*
Problem - Given a binary number ,help Von Neuman to find out its decimal representation.
For eg 000111 in binary is 7 in decimal.
Input Format - The first line contains N , the number of binary numbers. Next N lines contain N integers each representing binary represenation of number.
Constraints - N<=1000 Digits in binary representation is <=16.
Output Format - N lines,each containing a decimal equivalent of the binary number.
*/
#include<iostream>
using namespace std;
int main() {
int total_inputs, bin_val;
cin>>total_inputs;
while(total_inputs>0){
cin>>bin_val;
int temp = bin_val;
int base = 1;
int dec_val = 0;
while(temp>0){
int last_digit = temp%10; // Extract rightmost digit
dec_val = dec_val + (last_digit * base); // add the digit to answer
base = base * 2; // update the power of 2
temp = temp / 10; // update the number
}
cout<<dec_val<<endl;
total_inputs--;
}
return 0;
}
/*
Sample Input:
4
101
1111
00110
111111
Sample Output:
5
15
6
63
*/