Ask Question
11 October, 19:34

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

+2
Answers (1)
  1. 11 October, 22:07
    0
    The program to this question can be described as follows:

    Program:

    num = int (input ('Enter a number: ')) #input value by user

    st = '' #defining string variable

    while num > 0: #define loop to calculte values binary number

    val = num % 2 # holding remainder (0 or 1) in val variable

    st = st+str (val) # store value in str variable

    num = num//2 #calculte quotient value

    print ("binary number is: ", st)

    Output:

    Enter a number: 5

    binary number is: 101

    Explanation:

    Program description as follows:

    Firstly the "num" variable is declared, for user input, then an "st" variable is declared, to calculates user input value binary number.

    In the next step, a while loop is declared, inside the loop, another variable "Val" is declared, that holds value remainders, which is added on the "st" variable.

    Outside the loop, the print method is used, that prints st variable holds value.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “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 ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers