Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In Python Please. Write a program that asks the user to enter a positive integer that represents a number in the decimal system and then
In Python Please.
Write a program that asks the user to enter a positive integer that represents a number in the decimal system and then displays the binary representation of the number. Your solution must include at least the following function: decimalToBinary(number): a recursive function that takes a positive integer number in decimal) as its parameter and returns a string with it's binary representation. Recall from that in order to convert a decimal number to binary we repeatedly divide by 2 and then read the remainders backwards. Before you attempt to write the code for this problem, make sure that you identify the base case and the recursive case. Let the recursive case solve a simpler problem. The main part of the program should: Ask the user to enter a positive integer value, or O to end the program. Validate the user's input: o if the user enters a negative value, the program should issue an error message and ask the user to try again until a valid value is entered. o if the user enters a non-integer value, the program should issue an error message and ask the user to try again until a valid value is entered. Call the recursive function and print the binary value. Go back to ask the user to enter a positive integer value, or O to end the program. Below are some sample runs: Enter a positive integer or O to end: 8 The equivalent of 8 in Binary is 1000 Enter a positive integer or O to end: 2 The equivalent of 2 in Binary is 10 Enter a positive integer or O to end: 15 The equivalent of 15 in Binary is 1111 Enter a positive integer or O to end: 0 Enter a positive integer or 0 to end: abc Error: An integer value was expected. Try again Enter a positive integer or O to end: 1.24 Error: An integer value was expected. Try again Enter a positive integer or 0 to end: -9 Error: you entered a negative value. Try again Enter a positive integer or 0 to end: 25 The equivalent of 25 in Binary is 11001 Enter a positive integer or 0 to end: 0Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started