1. Write a recursive function to compute the binary equivalent of a given positive integer n. The recursive algorithm

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
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

Post by answerhappygod »

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. }
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply