Question: 5. ( Recursion.java ) Write a java program that has three static recursive methods and a main method. In your main method (5 marks) prompt

5. (Recursion.java) Write a java program that has three static recursive methods and a main method. In your main method (5 marks) prompt for user input and display the results of each of your recursive methods as shown in the sample output below.

a) Write a method that uses recursion to compute the value of , where a and n are both arguments to the method. If n = 0, the method should return 1 as . If n = 1, the method should return a as . If n is any other number thats for you to determine but remember, .

b) Write a method that uses recursion to return the reverse of a String that is passed to the method as an argument. Hint: For base cases, consider a string that has 1 or fewer charactershow much work is there to reverse them? Otherwise a reversed string is the last letter of the original string plus the reverse of the rest. Try it out on paper first.

c) Write a recursive method that determines the number of digits in an integer, n. Hint: If n < 10, there is one digit. Otherwise, it has one more digit than n / 10.

For these questions and with recursion in general, the trick is to determine 1) the base case(s) that will cause the function to simply return and end the recursion and 2) the recursive case(s) that will cause the function to call itself with a smaller version of the same problem. All input and output should take place in your main method. *** Dont forget to eat the new line if you use your scanner between numbers and strings.

java Recursion

Enter two numbers, base then exponent: 2 0

Result: 1

Enter a string to reverse: apples

Result: selppa

Enter a number: 123

Number of digits: 3

java Recursion

Enter two numbers, base then exponent: 100 1

Result: 100

Enter a string to reverse: Z

Result: Z

Enter a number: 6

Number of digits: 1

java Recursion

Enter two numbers, base then exponent: 2 12

Result: 4096

Enter a string to reverse: i luv cosc!

Result: !csoc vul i

Enter a number: 1234567890

Number of digits: 10

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!