1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
zhuklara [117]
3 years ago
7

Given non-negative integers x and n, x taken to the nth power can be defined as: x to the 0th power is 1 x to the nth power can

be obtained by multiplying x to the n-1'th power with x Write a long-valued function named power that accepts two int parameters x and n (in that order) and recursively calculates and returns the value of x taken to the n'th power.
Computers and Technology
1 answer:
saveliy_v [14]3 years ago
4 0

Answer:

The power function can be written as a recursive function (using Java) as follows:

  1. static int power(int x, int n)
  2. {
  3.        if(n == 0){
  4.            return 1;
  5.        }
  6.        else {
  7.            return power(x, n-1 ) * x;
  8.        }
  9. }

Explanation:

A recursive function is a function that call itself during run time.

Based on the question, we know x to the 0th power is 1. Hence, we can just create a condition if n = 0, return 1 (Line 3 - 5).

Next, we  implement the logic "x to the nth power can be obtained by multiplying x to the n-1'th power with x " from the question with the code:  return power(x, n-1 ) * x in the else block. (Line 6 -8)

In Line 7, power() function will call itself recursively by passing x and n-1 as arguments. Please note the value of n will be reduced by one for every round of recursive call. This recursive call will stop when n = 0.

Just imagine if we call the function as follows:

int result =  power(2,  3);

What happen  will be as follows:

  • run Line 7 -> return power(2, 2) * 2    
  • run Line 7 -> return power(2, 1) * 2
  • run Line 7 -> return power(1, 0) * 2
  • run Line 4 -> return 1    (Recursive call stop here)

Next, the return value from the inner most recursive call will be return to the previous call stack:

  • power(1, 0) * 2   ->   1 * 2
  • power(2, 1) * 2   ->   1 * 2 * 2
  • power(2, 2) * 2   ->   1 * 2 * 2 * 2 - > 8 (final output)  
You might be interested in
How could the following line of code be shortened?
Alinara [238K]

Answer:

2

Explanation:

The second option is the only one that will work. The last would work but doesn't make the code any shorter.

8 0
1 year ago
What will be the output of the following lines of code and the user's response?
Arte-miy333 [17]

Answer:

The answer is C. 85

Explanation:

The int() function is usually used to turn a float, to an int<em>.</em> When you use the int() function, it just cuts of everything past the decimal. It doesn't round the float. Leaving you with the option C. 85

hope this helped you :D

6 0
2 years ago
1. Describe the sequencing of factors influencing health status codes for patients that also have chronic conditions being manag
Vesnalui [34]
Bubyggyy tvtvtvttfrcrt
4 0
3 years ago
Why is it good to be a computer programmer?​
liubo4ka [24]

Answer:

It's your wish become other professions like doctor, engineer are also good. But in this profession you can make apps, websites and can do hacking. Which is are cool things to do. But every profession is best at it's profession.

Explanation:

If you like my answer than please mark me brainliest

5 0
2 years ago
How is unqualified assumptions a disadvantage of communication​
Eddi Din [679]

Answer:

Assumptions sabotage effective communication and have the potential to lead everyone down unintended paths

Explanation:

4 0
2 years ago
Other questions:
  • Computer hardware had been designed to run a single operating system and a single app, leaving computers vastly underutilized. o
    15·1 answer
  • DSL technology is less efficient for service providers in the United States because many of their customers live in less-densely
    11·1 answer
  • What do you click to create a new presentation in Normal view
    14·1 answer
  • HI GIVING BRAINLIEST Tyra used the software development life cycle to create a new game. She released it to her friends in order
    14·2 answers
  • How many cells does the organism have?\
    5·2 answers
  • How to shutdown a computer by step by step​
    5·2 answers
  • Which option identifies what the computer will yield in the following scenario?
    10·1 answer
  • What are the steps in preparing a bootable USB installer?​
    11·1 answer
  • How are digital and analog data similar? How are they different?
    15·1 answer
  • How do we ensure that future technologies are fair to society?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!