Instructions Using Recursion Create Definition Function Takes Int Determines Positive Powe Q37278473

Instructions: Using recursion, create a definition for thefunction below that takes an int and determines if it is a positivepower of 2. (2,4,8,128, 1024….)

bool IsPowerOfTwo(int n);

Rules: Must use recursion (the function must call itself), noloops whatsoever (for,while, etc), no global variables, and nousing pow() or anything other than ordinary arithmeticoperators.

——————————————————————————————

My code so far if it helps anybody:

//(Almost works, but doesn’t work with odd numbers because intsround down when divided)

bool IsPowerOfTwo(int n)  
{
   // base cases
   if (n == 2)
       return true;
   else if (n < 2)
       return false;

   // recursive step
   else
   {
       return IsPowerOfTwo(n/2);
   }
}


Answer


ANSWER:-

#include

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.