Question

In: Computer Science

Program 5: Decimal to Binary In Computer Science we often work with different number systems to...

Program 5: Decimal to Binary

In Computer Science we often work with different number systems to represent data. The most commonly used number system (which we refer to as "decimal") is a "base 10" number system, which means that we use the values 0-9 to represent all numbers.

The binary number system is a "base 2" number system, which means that we use the values 0 and 1 to represent all numbers. We refer to each value in a binary number as a "bit" (i.e. the number 0101 consists of 4 bits). Here are some numbers represented in both base 10 and base 2:

Decimal Binary
0 0
1 1
2 10
3 11
10 1010
25 11001
65 1000001
127 1111111
128 10000000

This is the algorithm for converting a decimal number into a binary number:

  • Attempt to divide the number by 2.
  • If there is a remainder you should record the bit 1.
  • If there is no remainder, you should record the bit 0.
  • Divide the number by 2 and throw away the remainder.
  • If the number is positive, repeat the process.
  • If the number is 0, you should end the process.

Note that each successive bit you compute is for the next highest place (the next highest power of 2) -- so it will be inserted at the front (or the left-end) of the binary number.

For example, consider the following decimal number:

decimal = 5

Here is how the algorithm works on this number:

    5 ÷ 2 = 2 remainder 1               Record this bit:    1
    5 // 2 = 2
    2 ÷ 2 = 1 remainder 0               Record this bit:   01
    2 // 2 = 1
    1 ÷ 2 = 0 remainder 1               Record this bit:  101
    1 // 2 = 0 (algorithm stops)

For this problem, write a program that prompts the user for an integer greater than or equal to zero. If the user supplies a value less than 0 you should re-prompt them. Then convert the number to binary, printing out each step in the process as you go.

Note: Your binary value can't be recorded as an integer. Since you are building the number up bit-by-bit, you will probably need to keep track of 0's that are in higher place values than the the highest 1 bit, in other words, leading zeros. Integers do not support leading 0's (i.e. 0001 will be reduced to 1). Therefore, for this program you should use a String to hold your bits (i.e. "0001" is allowed). You will need to figure out a way how to add bits to your String (hint: concatenation)

Here are a few sample runnings of your program:

Enter a number greater than or equal to 0: -5
Invalid, try again
Enter a number greater than or equal to 0: -9
Invalid, try again
Enter a number greater than or equal to 0: 9
9 % 2 = 1 ---> 1
9 // 2 = 4
4 % 2 = 0 ---> 01
4 // 2 = 2
2 % 2 = 0 ---> 001
2 // 2 = 1
1 % 2 = 1 ---> 1001
1 // 2 = 0

9 in binary is 1001
Enter a number greater than or equal to 0: 86
86 % 2 = 0 ---> 0
86 // 2 = 43
43 % 2 = 1 ---> 10
43 // 2 = 21
21 % 2 = 1 ---> 110
21 // 2 = 10
10 % 2 = 0 ---> 0110
10 // 2 = 5
5 % 2 = 1 ---> 10110
5 // 2 = 2
2 % 2 = 0 ---> 010110
2 // 2 = 1
1 % 2 = 1 ---> 1010110
1 // 2 = 0

86 in binary is 1010110
Enter a number greater than or equal to 0: 255
255 % 2 = 1 ---> 1
255 // 2 = 127
127 % 2 = 1 ---> 11
127 // 2 = 63
63 % 2 = 1 ---> 111
63 // 2 = 31
31 % 2 = 1 ---> 1111
31 // 2 = 15
15 % 2 = 1 ---> 11111
15 // 2 = 7
7 % 2 = 1 ---> 111111
7 // 2 = 3
3 % 2 = 1 ---> 1111111
3 // 2 = 1
1 % 2 = 1 ---> 11111111
1 // 2 = 0

255 in binary is 11111111
Enter a number greater than or equal to 0: 256
256 % 2 = 0 ---> 0
256 // 2 = 128
128 % 2 = 0 ---> 00
128 // 2 = 64
64 % 2 = 0 ---> 000
64 // 2 = 32
32 % 2 = 0 ---> 0000
32 // 2 = 16
16 % 2 = 0 ---> 00000
16 // 2 = 8
8 % 2 = 0 ---> 000000
8 // 2 = 4
4 % 2 = 0 ---> 0000000
4 // 2 = 2
2 % 2 = 0 ---> 00000000
2 // 2 = 1
1 % 2 = 1 ---> 100000000
1 // 2 = 0

256 in binary is 100000000

This program should be named as follows: LastNameFirstName_assign4_problem5.py (for example, "KappCraig_assign4_problem5.py")

Solutions

Expert Solution

Program

# loop to get the positive number
while True:
    number=int(input("Enter a number greater than or equal to 0: "))
    # if number is positive, exit loop
    if number>0:
        break
    # else print message and continue loop
    print("Invalid, try again")

binary = '' # string for bianry
n = number # n is number
# loop to calculate binary
while n>0:
    # find the quotient by dividing n by 2
    quotient=n//2
    # find the remainder by modulus n by 2
    remainder=n%2
    # append remainder to the left of binary string
    binary = str(remainder) + binary
    # print quotient and remainder
    print(n,"% 2 =",remainder,"--->",binary)
    print(n,"// 2 =",quotient)
    # assign quotient to n
    n = quotient
# print binary representation
print("\n",number,"in binary is",binary)

Program screeshot for indentation reference

Sample outpte 1

Sample output 2

Sample output 3

Sample output 4


Related Solutions

1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
Convert the decimal number, 315.56 into binary form?
Convert the decimal number, 315.56 into binary form?
Write a c++ program to convert any decimal number to either binary base  or Hex base...
Write a c++ program to convert any decimal number to either binary base  or Hex base number system. Test your program with the followings: Convert 15 from decimal to binary.  Convert 255 from decimal to binary. Convert BAC4 from hexadecimal to binary Convert DEED from hexadecimal to binary.  Convert 311 from decimal to hexadecimal. Convert your age to hexadecimal.
Program that does conversions (i.e. decimal to hexadecimal, binary to decimal) but needs to have a...
Program that does conversions (i.e. decimal to hexadecimal, binary to decimal) but needs to have a drop down menu to give you the option to choose with conversion you want to go from and convert into (JOptionPane). Cannot use the conversion tool that is built-in Java. Need to use equations or some sort. Please include comments on what is happening to understand.
represent the decimal number 101 and 6 as floating point binary numbers please show your work...
represent the decimal number 101 and 6 as floating point binary numbers please show your work and explained, I have a test.
Convert the following binary number to dotted decimal format. 11000000100110010000100101011001
Convert the following binary number to dotted decimal format. 11000000100110010000100101011001
Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
When we work with probabilities, we always use the decimal form. What's the decimal version of...
When we work with probabilities, we always use the decimal form. What's the decimal version of 15%? (Put a zero before the decimal point.) What about the decimal version of 0.3%? (Put a zero before the decimal point.) Suppose you roll a six-sided die and flip two coins. What is the chance that the die will come up as a 5 or a 6 and you'll get two tails? Express your answer as a value between 0 and 1, rounded...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT