1. Write a recursive function to compute the binary equivalent of a given positive integer n. The recursive algorithm
Posted: Sun May 15, 2022 1:30 pm
1. Write a recursive function to compute the
binary equivalent of a given positive integer n. The recursive
algorithm can be described in two sentences as follows.
Compute the binary equivalent of n/2.
Append 0 to it if n is even;
Append 1 to it if n is odd.
Use the following header for the function:
String binaryEquivalent(int n);
1. String toBinary(int n)
{
2. String lowBit = n%2==0 ? "0" :
"1";
3. if (n<2) return lowBit;
4. return toBinary(n/2) + lowBit;
5. }
binary equivalent of a given positive integer n. The recursive
algorithm can be described in two sentences as follows.
Compute the binary equivalent of n/2.
Append 0 to it if n is even;
Append 1 to it if n is odd.
Use the following header for the function:
String binaryEquivalent(int n);
1. String toBinary(int n)
{
2. String lowBit = n%2==0 ? "0" :
"1";
3. if (n<2) return lowBit;
4. return toBinary(n/2) + lowBit;
5. }