Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed
5. (Recursion.java) Write a java program that has three static recursive methods and a main method. In your main method (S marks) prompt for user imput and display the results of cach of your recursive methods as shown in the sample output below a) Write a method that uses recursion to compute the value of an, where a and n are both arguments to the method. I 0, the method should return 1 as a1. Ifn-1, the method should return a as aa. If n is any other mumber that's for you to determine but remember, a" x an-1 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 characters..how much work is there to reverse them? Otherwise a reversed string is the last letter of the original string plus the reverse of the res. Try it out on paper finst. e) Write a recursive method that determines the number of digits in an integer, n. Hint: Ifn10, 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 Don't forgct to "eat the new line" if you use your scanner between numbers and strings. java Recursion Enter two nunbers, base then exponent: 20 Result:1 Enter a atring to reverse: apples Result: selppa Enter a nunber: 123 Number of digits:3 java Recursion Enter two nunbers, base then exponent: 100 1 Result: 100 Enter a string to reversc: 2 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 Enter a number: 1234567890 Number of digits: 10

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

9. Describe the characteristics of power.

Answered: 1 week ago