Complete the definition of the following method. Your definition should be recursive. Unlike the method in Question 5, this method does not restrict the sign or value of its argument. You can use the same technique you used for Question 5, but you should have one more recursive case for negative exponents (Hints 104 is 1/10–6 for negative values of n. Also, if a is negative, –n is positive)
/**
Precondition: n can be any int.
Returns 10 to the power n.
*/
public static int computeTenToThe (int n)
Want to see the full answer?
Check out a sample textbook solutionChapter 11 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Additional Engineering Textbook Solutions
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Computer Science: An Overview (12th Edition)
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
Starting Out With Visual Basic (7th Edition)
C How to Program (8th Edition)
Starting Out with Java: From Control Structures through Objects (6th Edition)
- The following recursive method has been created. This method accepts two integer parameters called a and b respectively. Assume that the name of the method is m and the code in the recursive method is: if ( a > b ) return; if(a % 10 == 3) System.out.print(a+ " "); m(a+1, b); <---- recursive call } what would be the output with the following call m(1,50) where a is 1 and b is 50arrow_forwardChoose the correct one for the following recursive method when n is 3 int recursiveSum(int n) { if (n==0) return 0; return n+recursiveSum(n-1); } a. First and Last recursive call share the same copy of parameter n in memory. b. Every recursive call shares the same copy of parameter n in memory. c. There will be a separate copy of parameter n in memory for each recursive call. d. none of these e. Only First and Last recursive call have separate copies of parameter n in memory.arrow_forwardPlease explain the questions related to the code below: //1. Why is 20 printed 3 times and why does it start with 1, but end with 2? //2. What is the base case for this recursive method and why? //3. How would you implement this using a loop? //4. What is each if statement checking? //5. Would a solution to this using loops require more or less code and would it be more or less effecient? //6. Why is recursion a good choice to solve this problem? //7. How can recursion become infinite? //8. When is recursion appropriate? /* Load the code below into a cloud compiler and run each method one at a time by removing the comments for the method. Walk through the code as a group and explore how the code works. Submit the answers to the numbered questions for credit. */ public class DemoRecur { //Simple statement designed to show control flow in recursion public static void printDemo(int x, int max) {…arrow_forward
- The nth harmonic number is defined non-recursively as: H(n)=1+1/2+1/3+1/4+...+1/n Come up with a recursive definition and use it to guide you to write a method definition for a double-valued method named “harmonic” that accepts an int parameter n and recursively calculates and returns the nth harmonic number. Write a test program that displays the harmonic numbers, H(n)=1,2,3,4...10arrow_forwardhi I am writing a recursive method for the sum of a number, it works but I am unsure how it works and I would like to understand how it works? public static int sumDigits(int num) { if (num == 0) { return (0); } return (num%10) + sumDigits(num/10); }arrow_forwardPlease Give answer in C# Write a recursive method which sums all the even numbers up to a given number. For example if you call it with 10, it would return 30 because 10+8+6+4+2=30 Write a RECURSIVE method called sumEven It must take in an int (the max you wish to sum to) It must return an int (the sum) If it's passed in an even number it should sum the number passed in with all the even numbers before it. For example if passed 8, it would sum 8+6+4+2=20 If it's passed an odd number it should still sum only the EVEN numbers. What we mean is that sumEven(9) should return 8+6+4+2=20. Not 9+7+5+3+1. Hint: You'll have an if statement with the base condition, an else if for even and an else if for odd. In the case of odd, just call yourself with the next lowest even number. In your main method, open a file called "Carlton.txt" for writing. Use a loop to call the method above multiple times and print a line to the file for each iteration of the loop: The sum of even numbers up to 0 is 0 The…arrow_forward
- Java, Rewrite the following iterative method as a recursive method that computes the same thing. Note: your recursive method will require an extra parameter.public int iterative1(int x){int count = 0, factor = 2;while (factor < x){if (x % factor == 0) count++;factor++;}return count;}arrow_forwardJAVA Question 2: For two integers m and n, their GCD (Greatest Common Divisor) can be computed by a recursive method. Write a recursive method gcd(m,n) to find their Greatest Common Divisor. Method body: If m is 0, the method returns n. If n is 0, the method returns m. If neither is 0, the method can recursively calculate the Greatest Common Divisor with two smaller parameters: One is n, the second one is m mod n (or m % n). The recursive method cannot have loops. Note: although there are other approaches to calculate Greatest Common Divisor, please follow the instructions in this question, otherwise you will not get the credit. main method: Prompt and read in two numbers to find the greatest common divisor. Call the gcd method with the two numbers as its argument. Print the result to the monitor. Example program run: Enter m: 12 Enter n: 28 GCD(12,28) = 4 And here is what I have so far, package CSCI1302;import java.util.*;public class RecursionDemo { public static void…arrow_forwardThis program has a bug that leads to infinite recursion. Modify fn(int x, int y) method to fix the problem. Manually trace the recursive call, fn(2,3) and show the output (step by step). Can you identify the Base Case in recursive method fn(int x, int y)?arrow_forward
- Using JAVA Recursive Power Method Write a method called powCalthat uses recursion to raise a number to a power. The method should accept two arguments: The first argument is the exponentand the second argument is the number to be raised(example”powCal(10,2)means2^10). Assume that the exponent is anonnegative integer. Demonstrate the method in a program called Recursive (This means that you need to write a program that has at least two methods: mainand powCal. The powCal method is where you implement the requirements above and the main method is where you make a method call to demonstrate how your powCalmethod work).arrow_forwardWhich of the following is/are true regarding the characteristics of recursion? a.Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case. b.Recursive method requires less memory than an iterative method. c. It is possible to convert every recursive method to an iterative method. d.The method is implemented using an if-else or a switch statement that leads to different cases. e.One or more base cases are used to stop recursion.arrow_forwardAnswer usig Java The "odd/even factorial" of a positive integer n is represented as n!! and is defined non-recursively as: (n)(n-2)(n-4)...(4)(2) if n is even and is (n)(n-2)(n-4)...(5)(3)(1) if n is odd. For example7!!equals7*5*3*1or105 and 6!! equals 6*4*2 or 48. Come up with a recursive definition forn!! and use it to guide you to write a method definition for a method called oddevenfact that recursively calculates the odd/even factorial value of its single int parameter. The value returned by oddevenfact is a long.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education