Binary number to decimal number | Practice | GeeksforGeeks

Given a Binary Number B, find its decimal equivalent.
 
Example 1:
Input: B = 10001000
Output: 136

Example 2:
Input: B = 101100
Output: 44

 
Your Task:
You don't need to read or print anything. Your task is to complete the function binary_to_decimal() which takes the binary number as string input parameter and returns its decimal equivalent.
 
Expected Time Complexity: O(K * Log(K)) where K is number of bits in binary number.
Expected Space Complexity: O(1)
 
Constraints:
1 <= number of bits in binary number  <= 16
0
bucephalus370Premium1 week ago
public int binary_to_decimal(String str)   {       // Code here       int sum = 0;       int j = 0;       for (int i = str.length()-1; i>=0; i--){           int a = Character.getNumericValue(str.charAt(i));           sum += a*Math.pow(2,j);           j++;       }       return sum;   }
0
moonchild3263 weeks ago
def binary_to_decimal(self, str): # Code here return int(str,2)
0
siddharthkumar08071 month ago
int binary_to_decimal(string str) {     int base = 1;     int ans = 0;     int size = str.length()-1;     for(int i = size; i >= 0; i--)     {         ans += ((int)(str[i]-'0')*base);         base = base * 2;     }     return ans; }
0
soham0071 month ago
	int binary_to_decimal(string str)
		{
		    // Code here.
		    int dec =0;
		    int len = str.length();
		    for(int i=0;i<len;i++){
		        if(str[i]=='1') {
		            dec += pow(2,len-i-1);
		        }
		    }
		    
		    return dec;
		}
0
dell94591 month ago
Java:    public int binary_to_decimal(String str)   {       // Code here       return Integer.parseInt(str,2);   }
0
anushavennapoosa20022 months ago
class Solution{   public int binary_to_decimal(String str)   {       // Code here       int t=Integer.parseInt(str,2);       return t;   }}
0
bhanujggandhi2 months ago
// Idea is to iterate the string char one by one and check if it is 1 then we have to add appropriate power of 2 in the answer.
/*
1. Iterate over the string from any side, lets say from left until we reach the end.
2. If char at i is 1 then add 2^len-i-1 to the ans
3. Increment i;
4. Go to step 2.
*/

int binary_to_decimal(string str)
	{
		    int len = str.length();
		    int decimal = 0;
		    
		    for(int i=0; i<len;i++) {
		        if(str[i]=='1') 
		            decimal = decimal + pow(2, len - i - 1);
		    }
		    return decimal;
	}

+1
ashmeetsingh78012 months ago
int binary_to_decimal(string str) {      int n=str.length();           int m=n-1;           int ans=0;           for(int i=0;i<str.length();i++)           {               if(str[i]=='1')                {                   ans=ans+ pow(2,m);               }               m--;           }           return ans; }
+1
sana119038632 months ago
JAVA
 
class Solution
{
   public int binary_to_decimal(String str)
   {
       // Code here
       int sum =0,power=0;
       StringBuilder x = new StringBuilder(str);
         StringBuilder y = x.reverse();
         for(int i=0;i<y.length();i++){
             if(y.charAt(i)=='1'){
                power = (int) Math.pow(2,i); 
                sum = sum + power;
             }
         }
         
         return sum;
   }
}
 
+1
dwarampudi119049212 months ago
JAVA
class Solution
{
   public int binary_to_decimal(String str)
   {
     int z=0;
 int i=0;
 int x = str.length()-1;
 while(x>=0) {
  char s = str.charAt(x);
  z+=Integer.parseInt(Character.toString(s))*(int)Math.pow(2,i);
  x--;
  i++;
 }
 return z;
   }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.