-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBintoDec.cpp
49 lines (46 loc) · 975 Bytes
/
BintoDec.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
#include <bits/stdc++.h>
using namespace std;
int main()
{
unsigned long long decimal_num = 0; // final decimal number
string binary;
cout<<"Enter Binary Number"<<endl;
cin >> binary;
reverse(binary.begin(), binary.end());//reverse binary number
int size = binary.size();
for (int i = 0; i < size; i++)
{
if (binary[i] == '1')
{
decimal_num += pow(2, i);
}
}
cout << "Decimal Number is: " << decimal_num << endl;
return 0;
}
/*
Another Code:
//Suppose n=1010
int bintoDec(int n)
{
int ans = 0;
int x = 1;
while (n > 0)
{
//y=last digit
int y = n % 10; //y=0,1,0,1,10,10
ans += x * y; //ans=0,2,2,10
x*= 2; //x=2,4,8,16
//n=previous all digit of y
n /= 10; //101,10,1,0(break)
}
return ans;
}
int main()
{
int n;
cin >> n;
cout << bintoDec(n) << endl;
return 0;
}
*/