Question:

Consider the following iterative method powerOfTwo?

by  |  earlier

0 LIKES UnLike

Consider the following iterative method powerOfTwo, which uses repetitive multiplication for calculating the value of 2 to the power of n, where n is a positive integer number.

int powerOfTwo(int n)

{

int result = 1;

while (n > 0)

{

result *= 2;

n-- ;

}

return result;

}

Implement the recursive version of the method powerOfTwo.

 Tags:

   Report

1 ANSWERS


  1. int recursivePower(int n)

    {

    if(n == 0)

    {

    return 1;

    }

    else

    {

    return 2 * recursivePower(n - 1);

    }

    }

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.