UAB – 2005: Problem 3: Binary to Decimal Converter

A binary number is a sequence of bits (binary digits – 0’s and 1’s) of the form
BnBn-1……B1 B0, where each Bi is a bit.

The decimal equivalent is calculated by Bn * 2n+ Bn-1 * 2n-1 + … + B1 * 2 + B0.

Write a program to input a binary number and output the decimal equivalent.

The sample input will not have more than 8 individual bits (i.e., the largest value to be entered is 11111111).

Example 1

Enter binary number: 101
In decimal: 5

Example 2

Enter binary number: 11111
In decimal: 31

Enter binary number: 11111111
In decimal: 255
Enter binary number: 10101010
In decimal: 170
Enter binary number: 0
In decimal: 0
Enter binary number: 1
In decimal: 1
Enter binary number: 10
In decimal: 2
Enter binary number: 11
In decimal: 3
Enter binary number: 100
In decimal: 4
Enter binary number: 10000000
In decimal: 128


#include <cstdio>
#include <cstring>

using namespace std;

* we could use pow() function for calculating power
* pow() function returns double
* sometimes double can produce ambiguous result 
* for example, pow (2, 3) = 8.000000000001
* lets keep it simple and write our own power() function 
int power (int base, size_t exponent)
    int ret = 1;

    for ( int i = 1; i <= exponent; i++ ) {
        ret *= base;

    return ret;

int main (int argc, char *argv [])
    char binary [9 + 3];
    printf ("Enter binary number: ");
    scanf ("%s", binary);
    size_t length = strlen (binary);    // "size_t" is also known as "unsigned long"
    int decimal = 0;
    for ( int i = 0; i < length; i++ ) {
        int binaryDigit = binary [i] - '0';
        decimal += (binaryDigit * power(2, length - 1 - i));

    printf ("In decimal: %d\n", decimal);

    return 0;


Decimal to Binary conversion


#include <cstdio>
#include <algorithm>

using namespace std;

int main(int argc, const char * argv[])
    int n;

    scanf ("%d", &n);

    int binary [10 + 5];

    int index = 0;

    while (n) {
        binary [index] = n % 2;
        n /= 2;

    reverse(binary, binary + index);

    for ( int i = 0; i < index; i++ ) printf ("%d", binary [i]);

    if (index == 0) printf ("0");

    printf ("\n");

    return 0;


15 thoughts on “UAB – 2005: Problem 3: Binary to Decimal Converter

  1. I’m pretty new to programming, and here’s my contribution in Python. Sorry if my style is messy/confusing, as I’m not completely familiar with the conventions for commenting, spacing, etc. If you have any suggestions or simplifications, they’re definitely appreciated:

    #This program is designed to take a user-inputted binary number and convert it to decimal
    binary = raw_input('Please enter a binary number:  ') #asks user for binary input
    list1 = list(binary) #creates list from binary input
    a = 0 #sets initial lower bound of list slice to 0
    b = 1 #sets initial upper bound of list slice to 1
    n = len(list1) - 1 #specifies initial power of 2 as one less than length of binary input in list form
    B = binary[a:b]
    B2 = int(''.join(B)) * 2**n
    #print a, b
    #print n
    #print B2
    while b = 0:
        a += 1
        b += 1
        n -= 1
        #print a, b
        B  = binary[a:b]
        B2 = int(''.join(B)) * 2**n + B2
    print 'RESULT'
    print B2, 'is the binary number', binary, 'in decimal form.'
  2. Sorry for the really bad formatting of the above post, as well. I’m not positive how to format it properly so that it actually looks like code.

  3. @Colin,
    hi. you program doesn’t produce correct results
    for example, if input is 11 output should be 3
    and if input is 101 output should be 5

    try to debug your program .. or even better, write and justify your logic in a pen-paper

    for your commenting concern, i can say that, try to write code where you need to comment at minimal.
    what i mean is, your variable name should be more descriptive and your logic should be simple

    variable names, ‘a’, ‘b’ or ‘n’ doesn’t actually carry any information than ‘average’ / ‘input’ or ‘length’
    and one thing .. a famous jargon in coding world .. ‘your comment should ans the question ‘why’ .. not ‘what’
    just for example, ‘binary = raw_input(‘Please enter a binary number: ‘) #asks user for binary input’
    is an unnecessary comment .. we can all see you are taking input

    i am not a good programmer either .. still i hope the reply may help .. Best of luck to you🙂

  4. Actually, I just re-read the problem that you posted, and it says that it is solely supposed to be a bin to dec converter, and not both a dec to bin converter, as well. So when you initially put in the input of 11, you’re not going to get an output of 3. However, it does convert 101 to 5 quite capably, as I just ran my program again.

  5. @Colin
    yes the problem is all about binary to decimal conversion. you do not need to convert decimal to binary
    and well … binary (11) = decimal (3)

  6. we can do this simply:
    int main()
    int bin,dec,i,j,sum=0;
    printf(“Enter binary number:”);
    printf(“In decimal:%d”,sum);
    return 0;

  7. Am new to programming so can someone please explain what i did wrong
    string binary = Console.ReadLine();
    char[] character = binary.ToCharArray();
    string values = character.ToString();
    List componenets = new List();
    List results = new List();
    double Denary = 0;
    for (int i = 0; i < binary.Length; i++)
    componenets.Add(values.Substring(i, 1));
    foreach (string X in componenets)
    int numbers = int.Parse(X);
    for (int i = 0; i < binary.Length; i++)
    results.Add(numbers * Math.Pow(2, i));
    foreach (double Y in results)
    Denary = Denary + Y;

  8. @opeyemi
    can you debug your code and find out the mistake you have done? because it would be better if you can find out the error by yourself. if the conversion logic is clear to you then you should locate the error by debugging.

  9. Hello every one, I’ve been using python to deal with some simple problems, I am not a programmer and I am learning python for some time. This is my try in python:

    ui = raw_input(‘Enter a binary number :’)

    def b2d(s):
    dec = 0
    for i in range(1, len(s) + 1):
    #iterate on s in reversed order
    dec = dec + int(s[i*(-1)])*2**(i -1)
    return dec


  10. #include
    using namespace std;

    int calculatePower(int p)
    int pw=1;
    for(int i=1;i=1;i–)
    printf(“In decimal: %d”,sum);

    return 0;

  11. //binary to decimal convertion
    using namespace std;

    int calculatePower(int p)
    int pw=1;
    for(int i=1;i=1;i–)
    printf(“In decimal: %d”,sum);

    return 0;

  12. @Hassan Nomani great program. way better than mine, but it would still return a value even if I enter let’s say 50 or 200.
    I think you need to upgrade the code so that it doesnt return a value for non-binary inputs. do you know how that can be done?

  13. Hey Hassan, I’m new to the programming world, I have a doubt, you dint declare pow in your program. and I couldn’t find any functions like pow.. is it a default one..?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s