The following algorithm converts decimal to binary using repeated division by 2. Write
a program using any programming language to execute repeated division by 2.
Input: n [a nonnegative integer]
q := n, i := 0
[Repeatedly perform the integer division of q by 2 until q becomes 0. Store successive
remainders in a one-dimensional array r , r , r , . . . , r [k]. Even if the initialvalue of q equals 0, the loop should execute one time (so that r  is computed ). Thus
the guard condition for the while loop is i = 0 or q = 0.]
while (i = 0 or q = 0)
r [i] := q mod 2
q := q div 2
[r [i] and q can be obtained by calling the division algorithm.]
i := i + 1
[After execution of this step, the values of r , r , . . . , r [i − 1] are all 0’s and 1’s,
and a = (r [i − 1]r [i − 2] · · · r r r )2 .]
Output: r , r , r , . . . , r [i − 1] [a sequence of integers]
The following algorithm is division algorithm.
Write a program using any programming language for division algorithm.
[Given a nonnegative integer a and a positive integer d, the aim of the algorithm is to
ﬁnd integers q and r that satisfy the conditions a = dq + r and 0 ≤ r < d. This is done
by subtracting d repeatedly from a until the result is less than d but is still nonnegative.
0 ≤ a − d − d − d − · · · − d = a − dq < d.
The total number of d’s that are subtracted is the quotient q. The quantity a − dq equals
the remainder r .]
Input: a [a nonnegative integer], d [a positive integer]
r := a, q := 0
[Repeatedly subtract d from r until a number less than d is obtained. Add 1 to q each
time d is subtracted.]
while (r ≥ d)
r := r − d
q := q + 1
[After execution of the while loop, a = dq + r .]
Output: q, r [nonnegative integers]
Purchase answer to see full