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
iren2701 [21]
3 years ago
12

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 binar

y. For an integer x, the algorithm is: As long as x is greater than 0 Output x modulo 2 (remainder is either 0 or 1) Assign x with x divided by 2
Computers and Technology
1 answer:
Mashcka [7]3 years ago
6 0

Answer:

Following are the code to this question:

def binarynumber(num):#defining a method binarynumber that accepts a parameter num

   x=""#defining a string variable x

   if (num!=0):#defining if condition to check number not equal to 0

       while (num>=1):#defining a loop that check value is grater then equal to one

           if (num %2==0):#defining if condition to check num is even

               x=x+"0" #add string value 0 in num variable

               num=num/2 #divide the value by 2

           else:#defining else block

               x=x+"1"#add string value 1 in num variable

               num=(num-1)/2#first subtract 1 into num variable then divide the value by 2

   else:

       x="0"#assign string value 0 in num variable  

   return "".join(reversed(x))#return value

num = int (input ("Enter any number: "))#defining num variable that input the integer value

print (binarynumber(num))#using print method to call method binarynumber with passing num parameter

Output:

Enter any number: 12

1100

Explanation:

  • In the above python code a method "binarynumber" is declared, in which the "num" variable passes as the parameter inside the method a string variable "x" is declared that stores all converted values.
  • Inside the method and if the block is declared that checks number value is not equal to 0 if this condition is false then it will add string value and reverse its value.
  • Or if the condition is true it defines a while loop that calculates the given number binary digits and returns its value.
  • At the last step, the num variable is declared that inputs the integer value from the user end and calls the method by using the print method.    
You might be interested in
Which among the following choices is correct based on the two statements listed below? Statement 1: When the lexical analyzer sc
elena-14-01-66 [18.8K]

Answer: d. Statement 1 is an assertion, Statement 2 is the solution.

Explanation:

The Longest Match rule is a rule which states that whichever lexeme is scanned should always be determined on the support of the longest match available among all the tokens which is accessible. The lexical analyzer used should also follow the rules arrangement, where a reserved word, e.g., a keyword, in a language is given priority over the user input.

8 0
4 years ago
South Africa is the main supplier of which minerals in the world​
kenny6666 [7]

South Africa has the largest reserves of Platinum-group metals (PGMs; 88%), Manganese (80%), Chromite (72%) and Gold (13%) known reserves in the world. It is ranked second in Titanium minerals (10%), Zirconium (25%), Vanadium (32%), Vermiculite (40%) and Fluorspar (17%).

6 0
3 years ago
Read 2 more answers
Can you use a backup camera when you take your drivers test
Anon25 [30]
Yeah, because it's normal to use the backup cameras when driving and according to the law they cannot tell you want not to do as long as you yourself are not breaking any laws.
7 0
3 years ago
What phobia is a fear of computers or working on a computer?
aleksandr82 [10.1K]
Cyberphobia is a fear of computers
7 0
3 years ago
You are required to justify the need to implement a collapsed core network. Which justification below makes sense for a collapse
baherus [9]

Answer:

number 3

Explanation:

6 0
3 years ago
Other questions:
  • If you want a user to actively participate in an online activity, create a web ______________.
    12·2 answers
  • A(n) ____ occurs when a situation results in service disruptions for weeks or months, requiring a government to declare a state
    6·1 answer
  • Which is the best defense against ESD?
    9·2 answers
  • What term best describes when the scope of the project changes from what was initially agreed upon?
    8·1 answer
  • Give two examples of html structure
    15·1 answer
  • Given N lines of input, print the 3rd character from each line as a new line of output. It is guaranteed that each of the n line
    8·1 answer
  • Can you help me in this question
    13·1 answer
  • g Given the information below, answer each part of the question: Page Size: 4 KB a. How many bits are in the Page Offset
    11·1 answer
  • What is resource pooling?
    13·1 answer
  • A co-worker is called away for a short errand and leave the clinical PC logged onto the Confidential Information System. You nee
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!