Ask Question
14 July, 09:46

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

+3
Answers (1)
  1. 14 July, 12:17
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers