Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781119278023
Author: Michael T. Goodrich; Roberto Tamassia; Michael H. Goldwasser
Publisher: Wiley Global Education US
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
using r language
Describe a business example where referential integrity avoids data problems. specifying in the description of what the problems are and how they can be avoided. thaks
How do the concepts of balancing and leveling affect the process of creating a data model of a system? thanks
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
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,