-
Notifications
You must be signed in to change notification settings - Fork 617
/
binary_string_to_decimal.cpp
54 lines (40 loc) · 1.24 KB
/
binary_string_to_decimal.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
53
54
/*
A string containing only 0's and 1's is called a binary string.
The equivalent decimal of a binary number is the sum of 2 raised to the power of the positional values where 1 is present.
The least significant bit is assigned positional value 0.
The following program illustrates how to convert a binary string to its equivalent decimal
*/
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
string s;
// Taking input the binary string from the user
cin >> s;
// Assigning a variable to store the size of the string
int n = s.size();
// Initializing a variable named decimal to 0 which stores the decimal equivalent of the inputted binary string
int decimal = 0;
// Running a loop to convert the binary to its equivalent decimal
for(int i = n-1;i >= 0;i--)
{
// Updating the variable decimal if the ith position of the string has value '1'
if(s[i] == '1')
decimal += (int)pow(2,abs(i-(n-1)));
}
// Printing the equivalent decimal of the inputted binary string
cout << decimal;
return 0;
}
/*
TEST CASES:
Sample input : 1100
Standard output: 12
Sample input : 1000
Standard output: 8
Sample input : 100010
Standard output: 34
Time Complexity : O(n) where n is the size of string
Space Complexity: O(n)
*/