Binary to Decimal in Python, C++, Java, Php

You can find here how to convert from Binary to Decimal in C++, Java. If you want to convert from bin to dec, you can use our converter.

Programs for converting bin to dec

<?php 
// PHP program to convert binary to decimal 
  
// Function to convert from bin to dec 
function binToDec($number) 
{ 
  $dec_value = 0; 
  $base = 1; 
      
  $temp = $number; 
  while ($temp) 
  { 
    $digit = $temp % 10; 
    $temp = $temp / 10; 
          
    $dec_value += $digit * $base; 
    $base = $base * 2; 
  } 
  return $dec_value; 
} 
  
echo binToDec(110110),"\n"; 
?> 

/* C++ program to convert bin to dec */
#include <iostream>
using namespace std;
int main()
{
  long bin, dec = 0, rem, num, base = 1;
  bin = 01011; 
  while (num > 0)
  {
    rem = num % 10;
    dec = dec + rem * base;
    base = base * 2;
    num = num / 10;
  }
  
  cout << "Result: " << dec << endl;
  return 0;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>