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
AleksandrR [38]
3 years ago
8

Write a function named power that accepts two parameters containing integer values (x and n, in that order) and recursively calc

ulates and returns the value of x to the nth power.
Computers and Technology
1 answer:
lianna [129]3 years ago
8 0

Answer:

Following are the code to the given question:

int power(int x, int n)//defining a method power that accepts two integer parameters

{

if (n == 0)//defining if block to check n equal to 0

{

return 1; //return value 1

}

else//defining else block

{

x = x * power(x, --n); //use x variable to call method recursively

}

return x; //return x value

}

Explanation:

In the above-given code, a method power is defined that accepts two integer variable in its parameter, in the method a conditional statement is used which can be defined as follows:

  • In the if block, it checks "n" value, which is equal to 0. if the condition is true it will return value 1.
  • In the else block, an integer variable x is defined that calls the method recursively and return x value.
You might be interested in
Write a Java program to create a class called Cars. The class should include three instance variables: makes (type: String), mod
Fiesta28 [93]

Answer:

Explanation:

This code was written in Java. It creates the Cars class with the requested variables and methods. It also creates the TestCars class which asks the user for the necessary inputs and then creates three Cars objects and passes the input values to the constructors. Finally, it uses the show() method on each object to call the information. A test has been created and the output can be seen in the attached image below.

import java.util.Scanner;

class TestCars{

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       System.out.print("Enter Car Make: ");

       String make1 = in.next();

       System.out.print("Enter Car Model: ");

       String model1 = in.next();

       System.out.print("Enter Car Year: ");

       int year1 = in.nextInt();

       System.out.print("Enter Car Make: ");

       String make2 = in.next();

       System.out.print("Enter Car Model: ");

       String model2 = in.next();

       System.out.print("Enter Car Year: ");

       int year2 = in.nextInt();

       System.out.print("Enter Car Make: ");

       String make3 = in.next();

       System.out.print("Enter Car Model: ");

       String model3 = in.next();

       System.out.print("Enter Car Year: ");

       int year3 = in.nextInt();

       Cars car1 = new Cars(make1, model1, year1);

       Cars car2 = new Cars(make2, model2, year2);

       Cars car3 = new Cars(make3, model3, year3);

       car1.show();

       car2.show();

       car3.show();

   }

}

class Cars {

   String makes, models;

   int years;

   public Cars(String makes, String models, int years) {

       this.makes = makes;

       this.models = models;

       this.years = years;

   }

   public void show() {

       System.out.println("Car's make: " + this.makes);

       System.out.println("Car's model: " + this.models);

       System.out.println("Car's year: " + this.years);

   }

}

5 0
3 years ago
Which of the following is the biggest issue facing wireless communication today?
anygoal [31]

Answer:

threats to privacy

Explanation:

7 0
3 years ago
The faster the film (the higher the ISO), the more visible its grain. When you are using higher ISO, you are not just bringing g
NikAS [45]
I could not tell you
3 0
3 years ago
Read 2 more answers
PLEASE HELP!!!!!!!!!!!
Dafna1 [17]
I'd say Davis was trying to increase persuasive power. Given that he saw that nobody was really his friend because he was shy and quiet, he tried to persuade people to like him by going to the game and hanging out with people. 
4 0
3 years ago
Read 2 more answers
Do you think I should ask out myrandaduro2005
HACTEHA [7]

Answer:

If you want, your choice

3 0
3 years ago
Read 2 more answers
Other questions:
  • Write a program totake a depth (in kilometers) inside the earth as input data;compute
    15·1 answer
  • You have implemented a network where hosts are assigned specific roles, such as for file sharing and printing. Other hosts acces
    7·1 answer
  • 30
    5·1 answer
  • Write a program that reads two fractions such as 1/2 and 1/4 and computes and stores the sum, the difference, the product and th
    15·1 answer
  • Biography of bill gates
    8·2 answers
  • 2. Given the following list, write a snippet of code that would print the individual elements of the list using the indexes of t
    13·1 answer
  • What will be the values of ans, x, and y after the following statements are executed? int ans = 35, x = 50, y =50; if ( x >=
    7·2 answers
  • Write an algorithm to display your name 10 times​
    5·1 answer
  • Please tell fast plzzzzzzzz​
    9·2 answers
  • C++
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!