10. Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the corresponding binary number. Do the conversion by repeatedly dividing the number n by 2 using integer division, keepting track of the remainders, until the number is reduced to 0. The remainders written in reverse order form the binary number string. The following table (also shown in an earlier lab) illustrates the process.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Can u solve it in Python thank you 

10. Write a function decimalToBinary(n) that converts a positive decimal integer n to a string
representing the corresponding binary number. Do the conversion by repeatedly dividing the
number n by 2 using integer division, keepting track of the remainders, until the number is
reduced to 0. The remainders written in reverse order form the binary number string. The
following table (also shown in an earlier lab) illustrates the process.
N//2
N%2
2
1
2
1
1
1
0 (done)
Read upwards: 101 is 5
54
27
27
13
1
13
6
1
3
3
1
1
1
1
0 (done)
Read upwards: 110110 is 54
Do integer division of 5 by 2, so that N//2 is 2 with remainder 1. Now divide 2 by 2 to get 1
with remainder 0. Next divide 1 by 2 to get 0 with remainder 1. Concatenating the remainders
in reverse order makes 101, which is 5 in binary. Another example: N = 54. The division
sequence is 54, 27, 13, 6, 3, 1, 0; and the remainder sequence is 0, 1, 1, 0, 1, 1. The remainders
concatenated in reverse order produce: 110110, which is 54 in binary. Write a program which
converts the values from 0 to 9 to binary, using the decimalToBinary(n) function. The output
should look like:
O is the binary of 0
1 is the binary of 1
10 is the binary of 2
11 is the binary of 3
100 is the binary of 4
101 is the binary of 5
110 is the binary of 6
111 is the binary of 7
1000 is the binary of 8
1001 is the binary of 9
Transcribed Image Text:10. Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the corresponding binary number. Do the conversion by repeatedly dividing the number n by 2 using integer division, keepting track of the remainders, until the number is reduced to 0. The remainders written in reverse order form the binary number string. The following table (also shown in an earlier lab) illustrates the process. N//2 N%2 2 1 2 1 1 1 0 (done) Read upwards: 101 is 5 54 27 27 13 1 13 6 1 3 3 1 1 1 1 0 (done) Read upwards: 110110 is 54 Do integer division of 5 by 2, so that N//2 is 2 with remainder 1. Now divide 2 by 2 to get 1 with remainder 0. Next divide 1 by 2 to get 0 with remainder 1. Concatenating the remainders in reverse order makes 101, which is 5 in binary. Another example: N = 54. The division sequence is 54, 27, 13, 6, 3, 1, 0; and the remainder sequence is 0, 1, 1, 0, 1, 1. The remainders concatenated in reverse order produce: 110110, which is 54 in binary. Write a program which converts the values from 0 to 9 to binary, using the decimalToBinary(n) function. The output should look like: O is the binary of 0 1 is the binary of 1 10 is the binary of 2 11 is the binary of 3 100 is the binary of 4 101 is the binary of 5 110 is the binary of 6 111 is the binary of 7 1000 is the binary of 8 1001 is the binary of 9
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education