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]
2 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]2 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
Which of the following is NOT an example of a font style?
erastovalidia [21]
C. Underline because it is not a kind of font. it is an effect.
3 0
3 years ago
Suppose you are given a sequence that is described by a formula, starting at index n=0. If you need to change the starting index
Arisa [49]

Answer:

n+1

Explanation:

Given

n = 0 --- starting index

Required

Change the starting index to n = 1

We have:

n = 0

To change the starting index to k, we simply rewrite as:

n+k

<em>In this case; k=1; so, the starting index will be: </em>n+1<em />

3 0
2 years ago
Which table option automatically adjusts column widths to fit cell content? autofit contents merge cells autofit window fixed co
noname [10]
Which table option automatically adjusts column widths to fit cell content?
autofit contents
6 0
3 years ago
01001000 01100101 01101100 01101100 01101111 00100000 01101101 01111001 00100000 01101110 01100001 01101101 01100101 00100000 01
Marianna [84]

You said Hello my name is REE

6 0
2 years ago
A physical cpu core without hyper-threading enabled can process two instructions at the same time.
diamong [38]

A physical CPU core without hyper-threading enabled can process two instructions at the same time is a false statement.

<h3>Can a CPU do multiple things at once?</h3>

Computers are those that do only one task (or process) at a single time. But a computer can alter tasks very fast and can do a lot of work.

The Central processing unit is known to be the brain of the computer system and without it, the computer cannot function or be turn on.

Hence, A physical CPU core without hyper-threading enabled can process two instructions at the same time is a false statement.

Learn more about CPU from

brainly.com/question/474553

#SPJ1

3 0
1 year ago
Other questions:
  • In 1–2 sentences describe how you would insert a row in a spreadsheet.
    6·2 answers
  • What is the service provided by a third party (such as an ISP) that enables you to connect another cloud directly to your Google
    15·1 answer
  • - Explain the Windows User Authentication process for Client connecting to the domain
    11·1 answer
  • Which of the following is a technique for storing or copying log events to acentralized logging server?a. Syslogb. Write­once re
    8·1 answer
  • When installing a device driver, start the computer in
    6·1 answer
  • George is sketching a wireframe representation of the home page of his website. What aspect of the home page would be impossible
    11·1 answer
  • Label provides the code that executes if no case label is matched ​
    6·1 answer
  • PLS HELP ILL GIVE BRAINLY- (enter the answer) Microsoft _________ is an example of a desktop publishing software
    13·2 answers
  • What is the usual price of smartphone apps?
    8·2 answers
  • Trojans depend on ________ to spread. A rootkits B self-replication C code injection D social engineering
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!