Q4)Given a non-signed decimal integer M, we would like to convert it into binary
ID: 3645633 • Letter: Q
Question
Q4)Given a non-signed decimal integer M, we would like to convert itinto binary. For example, the decimal number "13" is equivalent to
the binary number 1101
b) Design an algorithm by induction to generate (and print) the binary
digits of a given non-signed decimal number M.
Write a program to implement your algorithm for Q4(b). Test your program
using at least 6 different inputs. In the document to be uploaded,
include a copy of your your program, each input tested, and the
binary number generated.
Explanation / Answer
i ll just tell you the algorithm 1- keep a counter r , initialized to r = 0 and a integer B also initialized to 0 2 - divide M by 2 3 - store remainder in R 4 - add R*10^r to B 5 - increase count 6 - Repeat from 2 until M = 0 you will get binary equivalent of M in B in psuedo code : int r=0,B=0 for (M >0) { R = M%2 M = M/2 B = B + R*10^r r++ } you just have to add steps to take input into M from user and to print the result B
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.