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

Further practice on String interpretation, loops, and your binary skills. This question is the opposite of the Binary to Decimal exercise:

Write a program that asks for a (whole) decimal number from the user.
Store this input. Calculate the binary equivalent of the users decimal input. Try to avoid using built in formatting features that will make this exercise too simple. |

Sample I/O:

“Please enter a decimal number”

8

8 as a decimal = 1000 as binary

OR

171

171 as a decimal = 10101011 as binary

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

“Please enter a decimal number”

8

8 as a decimal = 1000 as binary

OR

171

171 as a decimal = 10101011 as binary

If you don’t know how to convert from decimal to binary 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 22 lines of code.

Create a string that will have 1s and 0s appended as the code runs.

Find the largest value of 2^x that, when subtracted from the input, won’t result in a negative number.

Loop from the power you found down to 2^0, altering numbers and strings as you go.

## C#:## Show Code >>Console.WriteLine("Please enter a decimal number"); String decimalString = Console.ReadLine(); //If you want error checking this is where to do it double decimalNumber = Convert.ToDouble(decimalString); //store it so that we can give a neat output double keepForOutput = decimalNumber; String binaryString = ""; //the string that will be built up int power = 0; //used to determine what power to raise to //find the largest value of 2^power that when subtracted from decimalNumber won't result in a negative number while(Math.Pow(2,power+1) <= decimalNumber) power = power + 1; //loop down through the powers until they are negative while(power >= 0) { //check if the number will result in a negative if (decimalNumber - Math.Pow(2, power) >= 0) { //if not change decimal number and add a 1 decimalNumber = decimalNumber - Math.Pow(2,power); binaryString = binaryString + "1"; } else { //otherwise add a 1 binaryString = binaryString + "0"; } power--; } //Output the results: Console.WriteLine(keepForOutput+" as a decimal = "+ binaryString+" as binary"); Console.WriteLine("Press enter to exit"); Console.ReadLine(); |

## Java:## Show Code >>Scanner sc = new Scanner(System.in); System.out.println("Please enter a decimal number"); double decimalNumber = sc.nextInt(); //store it so that we can give a neat output double keepForOutput = decimalNumber; //If you want error checking this is where to do it String binaryString = ""; //the string that will be built up int power = 0; //used to determine what power to raise to //find the largest value of 2^power that when subtracted from decimalNumber won't result in a negative number while(Math.pow(2,power+1) <= decimalNumber) power = power + 1; //loop down through the powers until they are negative while(power >= 0) { //check if the number will result in a negative if (decimalNumber - Math.pow(2, power) >= 0) { //if not change decimal number and add a 1 decimalNumber = decimalNumber - Math.pow(2,power); binaryString = binaryString + "1"; } else { //otherwise add a 1 binaryString = binaryString + "0"; } power--; } //Output the results: System.out.println(keepForOutput+" as a decimal = "+ binaryString+" as binary"); System.out.println("Exiting now"); |

## Python:## Show Code >>#get the input decimalNumber = int(input("Please enter a decimal number")) keepForOutput = decimalNumber #store it so that we can give a neat output #if you want error checking this is where to put it binaryString = "" #the string that will be built up power = 0 #used to determine what power to raise to #find the largest value of 2^power that when subtracted from decimalNumber won't result in a negative number while(pow(2,power+1) <= decimalNumber): power = power + 1 #loop down through the powers while(power >= 0): #check if the number will result in a negative if (decimalNumber - pow(2,power) >= 0): #if not change decimal number and add a 1 decimalNumber = decimalNumber - pow(2,power) binaryString = binaryString + "1" else: #otherwise add a 1 binaryString = binaryString + "0" power = power - 1 print(keepForOutput,"as a decimal =", binaryString,"as binary") |

Updated 15 August 2015 by Matthew F.