1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Damm [24]
3 years ago
5

6.23 LAB: Convert to binary - functions Instructor note: This is a lab from a previous chapter that now requires the use of a fu

nction. If you weren't able to solve it before, think of this as a new opportunity. Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string. Ex: If the input is: 6 the output is: 110 Your program must define and call the following two functions. The function integer_to_reverse_binary() should return a string of 1's and 0's representing the integer in binary (in reverse). The function reverse_string() should return a string representing the input string in reverse. def integer_to_reverse_binary(integer_value) def reverse_string(input_string)
Computers and Technology
1 answer:
Kitty [74]3 years ago
3 0

Answer:

The program in Python is as follows:

import math

def integer_to_reverse_binary(integer_value):

   remainder = ""

   while integer_value>=1:

       remainder+=str(integer_value % 2)

       integer_value=math.floor(integer_value/2)

   reverse_string(remainder)

def reverse_string(input_string):

   binaryOutput=""

   for i in range(len(input_string)-1,-1,-1):

       binaryOutput = binaryOutput + input_string[i]

   print(binaryOutput)

integer_value = int(input("Enter a Number : "))

integer_to_reverse_binary(integer_value)

Explanation:

This imports the math module

import math

This defines the integer_to_reverse_binary function

def integer_to_reverse_binary(integer_value):

This initializes the remainder to an empty string

   remainder = ""

This loop is repeated while the integer input is greater than or equal to 1

   while integer_value>=1:

This calculates the remainder after the integer value is divided by 2

       remainder+=str(integer_value % 2)

This gets the floor division of the integer input by 2

       integer_value=math.floor(integer_value/2)

This passes the remainder to the reverse_string function

   reverse_string(remainder)

The reverse_string function begins here

def reverse_string(input_string):

This initializes the binaryOutput to an empty string

   binaryOutput=""

This iterates through the input string i.e. the string of the remainders

   for i in range(len(input_string)-1,-1,-1):

This reverses the input string

       binaryOutput = binaryOutput + input_string[i]

This prints the binary output

   print(binaryOutput)

<u>The main begins here</u>

This gets input for integer_value

integer_value = int(input("Enter a Number : "))

This passes the integer value to the integer_to_reverse_binary function

integer_to_reverse_binary(integer_value)

You might be interested in
Emotional intelligence is a new term to describe personal traits ?
Alex Ar [27]
This sentence is false
8 0
3 years ago
The chain of _____ documents that the evidence was under strict control at all times and no unauthorized person was given the op
Arturiano [62]

Answer:

custody.

I hope I could help

4 0
2 years ago
Can you please answer the question in the picture ! :)
Ilya [14]

Answer:

Explanation: If and else statements are one of the most important parts of programming.

1. You add an if you do something if a condition is met. For example, if number of chocolates equals zero( a condition), and inside you can do something like buy more chocolates. And else statement happens if the if condition isn't met and you want to the code to do something else. You don't need this if you don't want anything to happen if the if condition isn't true.

2. This might be used in a number guessing game. Let's say you have to guess a number, if its correct the computer should correct but if you get it wrong it should display wrong. You can write if the number the user writes in equal to the number you are supposed to guess say correct. Else say wrong

Hope this helps. Please mark as brainliest! Thanks!

4 0
3 years ago
45 points!
sertanlavr [38]
Email and cookies need SSL. Other options do not need SSL
5 0
2 years ago
Describe the procedure for creating a table or spreadsheet in a presentation slide.
brilliants [131]
<span>Select the slide that you want to insert a table on.On the Insert tab, in the Tables group, click Table, and then click Excel Spreadsheet.<span>To add text to a table cell, click the cell, and then enter your text</span></span>
5 0
3 years ago
Other questions:
  • George is a contractor who creates websites and web applications. What is George working as?
    7·2 answers
  • Match the number in the picture to the correct Excel interface part. Each number is only used one time.
    11·1 answer
  • 4. Together, what do the divisions of the DHSMV do? A. Make sure lines are long B. Use more money than other government organiza
    14·1 answer
  • The mobile nodes (devices) add or leave a Mobile Ad-hoc Network, changing the _____ of this network over time. a) infrastructure
    14·1 answer
  • Which value can be entered to cause the following code segment to display the message "That number is acceptable."? ____.
    15·1 answer
  • How do I keep my computer and data safe and secure while using the Internet?
    14·2 answers
  • Think of some local businesses that have websites. Look online and identify two different websites for businesses or services in
    14·1 answer
  • PLEASE HELP
    12·1 answer
  • Match the TCP/IP Layer to a problem that can happen there.
    8·1 answer
  • ________(fill in the blank)in online education is intrinsically related to equity.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!