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
The Polish mathematician Wacław Sierpiński described the pattern in 1915, but it has appeared in Italian art since the 13th cent
soldi70 [24.7K]

Answer:

/ Sierpinski.java

public class Sierpinski {

     // method to find the height of an equilateral triangle with side length =

     // length

     public static double height(double length) {

           // formula= length*sqrt(3)/2

           double h = (length * Math.sqrt(3)) / 2.0;

           return h;

     }

     // method to draw a filled triangle (pointed downwards) with bottom vertex

     // x,y

     public static void filledTriangle(double x, double y, double length) {

           // finding height

           double h = height(length);

           // filling triangle as a polygon

           // passing x, x-length/2, x+length/2 as x coordinates

           // and y, y+h, y+h as y coordinates

           StdDraw.filledPolygon(new double[] { x, x - (length / 2.0),

                       x + (length / 2.0) }, new double[] { y, y + h, y + h });

     }

     // method to draw an n level sierpinski triangle

     public static void sierpinski(int n, double x, double y, double length) {

           // checking if n is above 0 (base condition)

           if (n > 0) {

                 // drawing a filled triangle with x, y as bottom coordinate, length

                 // as side length

                 filledTriangle(x, y, length);

                 // drawing the triangle(s) on the top

                 sierpinski(n - 1, x, y + height(length), length / 2);

                 // drawing the triangle(s) on the left side

                 sierpinski(n - 1, x - (length / 2.0), y, length / 2);

                 // drawing the triangle(s) on the right side

                 sierpinski(n - 1, x + (length / 2.0), y, length / 2);

           }

     }

     public static void main(String[] args) {

           // parsing first command line argument as integer, if you dont provide

           // the value while running the program, this program will cause

           // exception.

           int n = Integer.parseInt(args[0]);

           // length of the outline triangle

           double length = 1;

           // finding height

           double h = height(length);

           // drawing a triangle (pointed upwards) to represent the outline.

           StdDraw.polygon(new double[] { 0, length / 2, length }, new double[] {

                       0, h, 0 });

           // drawing n level sierpinski triangle(s) with length / 2, 0 as x,y

           // coordinates and length / 2 as initial side length

           sierpinski(n, length / 2, 0, length / 2);

     }

}

7 0
2 years ago
Which of the following correctly stores 45 squared in the variable x?
timama [110]

Answer:

x = 45

x = x ** 2

6 0
3 years ago
When you save something to the Desktop on a school computer, what drive letter will it save to
Natali [406]

When you save something to the Desktop on a school computer, the drive letter it will save to the C drive.

<h3>What is a C drive?</h3>

C drive is that part of the computer that contains the operating system and files of the system. The files on which we worked are saved on the C drive of the system.

This is a type of hard drive. The work we have done on the system is automatically saved on the drive. We can easily find the C drive on the computer's file explorer. It automatically saved the data, but you can save manually the data of the D drive.

Thus, the drive letter it will save to the C drive.

To learn more about C drive, refer to the link:

brainly.com/question/2619161

#SPJ1

4 0
1 year ago
What procedures are involved in saving a file for the first time
ratelena [41]
You must name the file and you must also choose where the file will be saved.

3 0
3 years ago
Help. serious help. the acc im using right now cant be ridded of by myself from being unable to have a pass. i need to contact a
anzhelika [568]

Answer:

Can't really help you... But you can try emailing support.

7 0
2 years ago
Other questions:
  • In a paragraph of no less than 125 words, describe how technology helps business professionals to be more efficient. Include exa
    12·2 answers
  • Remy’s manager has asked him to change the background color scheme from reds to blues in the standard template the company uses
    15·2 answers
  • Prove each statement using a proof by exhaustion. For every integer n such that 0 lessthanorequalto n &lt; 3, (n + 1)^2 &gt; n^3
    9·1 answer
  • Can somebody help me out
    15·1 answer
  • We need an equals method for the Dog class. It needs to give back to the caller a boolean value indicating whether another objec
    5·1 answer
  • Help me to write spaghetti stack function, please!!
    11·1 answer
  • Unlike radio frequency identification (RFID) tags, bar codes: Question 30 options: require a reader that tunes into a specific f
    14·1 answer
  • Does analogue conversation take place in source as transmitter?
    5·2 answers
  • What is the benefit of hosting a website on a personali
    6·1 answer
  • The page that appears when you first open your Internet browser is the _____.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!