## Binary to Decimal |
## Loading... |

Time to practice String interpretation as well as your binary skills:

Write a program that asks for a binary number from the user.
Store this input as a string. By going through each character one at a time, calculate the decimal equivalent of the user's binary input. Try to avoid built in formatting features that will make this exercise too simple. |

Sample I/O:

“Please enter a binary number”

1000

1000 as binary = 8 as decimal

OR

10101010

10101010 as binary = 170 as decimal

If you don't know what binary is… you probably shouldn't be here. But don’t worry, there’s still time to find out from here.

If you don’t know how to convert from binary to decimal have a look here.

“Please enter a binary number”

1000

1000 as binary = 8 as decimal

OR

10101010

10101010 as binary = 170 as decimal

If you don't know what binary is… you probably shouldn't be here. But don’t worry, there’s still time to find out from here.

If you don’t know how to convert from binary to decimal have a look here.

*Try not to look at the answers until you've solved the problem*### Show Hints>>

This problem can be solved in under 15 lines of code

Look at each character one at a time and create a running total.

Starting at the end of the String might be easier than the alternative.

You need to keep track of (or calculate for each iteration) a power value.

2^power * character(0 or 1) = value in decimal of a single character.

## C#:## Show Code >>Console.WriteLine("Please enter a binary number"); String binaryString = Console.ReadLine(); //If you want error checking (like making sure binaryString contains only //only 1's and 0's)...this is where to do it int indexCounter = binaryString.Length-1; //starts at the end of the string int power = 0; //used to determine what power to raise to double sum = 0; //contains the total so far //loop from the end of the input down to the start while (indexCounter >= 0) { sum = sum + Math.Pow(2, power) * int.Parse(binaryString[indexCounter].ToString()); //broken down = sum + (2^power) * (char at indexCounter convertered to an int) indexCounter = indexCounter - 1; power = power + 1; } //Output the results: Console.WriteLine(binaryString + " as binary = " + sum + " as decimal"); Console.WriteLine("Press enter to exit"); Console.ReadLine(); |

## Java:## Show Code >>Scanner sc = new Scanner(System.in); System.out.println("Please enter a binary number"); String binaryString = sc.next(); //If you want error checking (like making sure binaryString contains only //only 1's and 0's)...this is where to do it int indexCounter = binaryString.length()-1; //starts at the end of the string int power = 0; //used to determine what power to raise to double sum = 0; //contains the total so far Character numAsChar; int numAsInt; //loop from the end of the input down to the start while (indexCounter >= 0) { //Get the charachter at a certain position numAsChar = binaryString.charAt(indexCounter); //changes the charachter into an integer, now its ready for multiplying //the following 2 lines can be compressed into just 1 line numAsInt = Integer.parseInt(numAsChar.toString()); sum = sum + Math.pow(2, power)*numAsInt; indexCounter = indexCounter - 1; power = power + 1; } //Output the results: System.out.println(binaryString+" as binary = "+sum+" as decimal"); System.out.println("Exiting now"); |

## Python:## Show Code >>#get the input binaryString = str(input("Please enter a binary number")) #if you want error checking this is where to put it indexCounter = len(binaryString)-1 #starts at the end of the string power = 0 #used to determine what power to raise to sum = 0 #contains the total so far #loop from the end of the input down to the start while (indexCounter >= 0): sum = sum + pow(2,power)*int(binaryString[indexCounter]) indexCounter = indexCounter - 1 power = power + 1 #Output the results: print(binaryString,"as binary =",sum,"as decimal") |

Updated 1 March 2015 by Matthew F.