Ask 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 integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x

+5
Answers (1)
  1. 5 December, 18:21
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers