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
Look at the following structure declaration.
myrzilka [38]

Answer:

B. a tag

Explanation:

In the structure definition given below:

struct Employee

{

string name;

int idNum;  

};

Employee corresponds to the tag of the structure. The tag is used to create additional instances of the structure. For example:

struct Employee e1;

struct Employee e2;

name and idNum are members of the Employee structure and are referenced using the dot notation. e.g.,

struct Employee e1;

e1.idNum=1;

7 0
3 years ago
Lael Masterson works in the Student Activities Office at Valerian State College in Illinois. Lael has started compiling informat
Nina [5.8K]

Answer:

Use the HLOOKUP function in the cell E2 by specifying the student's hourly rate value, the range of cells of the table holding the information, the row from which to return the value, and a value of true or false for approximate or exact match respectively.

Explanation:

Microsoft Excel is a statistics tool used for data analysis. It has several functions and graphical properties for cleaning and representing data.

The HLOOKUP function is an example of a search function in excel, it returns a value based on another value from a range of cells. The function accepts four arguments, they are, the value to be searched, the range table or cell range to search, the row to get the search value, and a mode of return value to return, could be either true for an approximate match or false for an exact match.

6 0
3 years ago
Servers that exist within a data center that is publicly accessible on the internet are referred to as on-premises servers.
adoni [48]

Servers that exist within a data center that is publicly accessible on the internet are referred to as on-premises servers: b. false.

<h3>What is a server?</h3>

A server can be defined as a dedicated computer system that is designed and developed to provide specific services to other computer devices or programs, which are commonly referred to as the clients.

<h3>What is an on-premises server?</h3>

An on-premises server can be defined as a type of server that are privately owned by a company or individuals, which must be managed and maintained individually.

In conclusion, servers that exist within a data center that is not publicly accessible on the internet are generally referred to as on-premises servers.

Read more on servers here: brainly.com/question/27960093

#SPJ1

7 0
1 year ago
A termination condition in a loop is analogous to________in a recursive method.
Varvara68 [4.7K]

Answer:

b) iteration.

Explanation:

There are various algorithms which are used in computer programming. Iteration is also a type of algorithm which is used to create loops. The instructions are inserted once and then these instructions are repeated to create a loop function.

8 0
3 years ago
What function would you use to calculate the total interest paid for the first year of a mortgage?.
WITCHER [35]

Answer:

To calculate the total interest paid for the first year of a mortgage, the formula M = [P.r (1+r)n] / [(1+r)n-1] × 12 can be used.

Explanation:

3 0
2 years ago
Other questions:
  • In this code, identify the repeated pattern and replace it with a function called month_days, that receives the name of the mont
    14·1 answer
  • Which action could be used to determine if a host is compromised and flooding traffic onto the network?
    6·1 answer
  • Samantha plans an investigation in which she will study a population of animals. Which of these answers best describes the focus
    8·1 answer
  • 7. Which innovation in video games do you think has been most significant? Include at least one way that innovation affects the
    6·1 answer
  • In excel, which symbol means not equal to when comparing two values?
    15·1 answer
  • What is a traffic controller?what are its functions?
    13·1 answer
  • Write the Q basic program to find the area of room.​
    10·1 answer
  • 7.4 Lesson Practice (projectstem): what is output if the user is enters 2?​
    8·1 answer
  • This unintelligent brain of mine decided to charge my phone overnight thinking that nothing bad will happen ;-; the next morning
    11·1 answer
  • In Python write a program that reads a list of integers into a list as long as the integers are greater than zero, then outputs
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!