Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
Expert Solution & Answer
Book Icon
Chapter 5, Problem 9R

Explanation of Solution

Non-recursive method for power:

The non-recursive method for power is given below:

Create the method power() that accepts the input parameter as “num” and “pow” to calculate and return the power of number.

//Define the power() method

public static double power(double num, int pow)

{

  //Declare and initialize the variables

  int n = 0;

  double ans = 1;

  //Loop executes until the power value

  while ((1 << n) <= pow)

  //Increment the "n" by "1"

  n++;

  //Loop executes until the power value

  for (int i= n-1; i >= 0; --i)

  {

  //Multiply "ans" with "ans"

  ans *= ans;

  //Check bitwise "i" and "pow" is greater than "0"

...

Blurred answer
Students have asked these similar questions
Design and draw a high-level "as-is" process diagram that illustrates a current process related to a product or service offered through the SSDCI.gov database.
Compare last-mile connections for connecting homes and businesses to the Internet
Explain wireless networking standards
Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,