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. }
1. Write a recursive function to compute the binary equivalent of a given positive integer n. The recursive algorithm
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
1. Write a recursive function to compute the binary equivalent of a given positive integer n. The recursive algorithm
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!