Question

Recursive Tracing. For each call to the following method, indicate what value is returned: public static...

Recursive Tracing. For each call to the following method, indicate what value is returned:

public static int mystery(int n) {

if (n < 0) {

return -mystery(-n);

} else if (n == 0) {

return 0;

} else {

return mystery(n / 10) * 10 + 9 - (n % 10);

}

Call

Value Returned

mystery(0)

mystery(5)

mystery(13)

mystery(297)

mystery(-3456)

}

Can any one help me with it?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here a new java program with name "main.java" is created, which contains following code.

main.java :

//Main class
public class Main
{ //Main method
   public static void main(String[] args) {
   //call to method
       System.out.println("mystery(5) : "+mystery(5));
   }
   //method defination
   public static int mystery(int n)
   { //if n is less than 0
if (n < 0)
{
return -mystery(-n);
}
//if n=0
else if (n==0)
{
return 0; //return 0
}
else
{ //return recursive call
return mystery(n / 10) * 10 + 9 - (n % 10);
}
   }
}

======================================================

Output : Compile and Run above program , will get the screen as shown below

Screen 1 :Screen when mystery(0) called

Screen 2 :Screen when mystery(5) called

Screen 3 :Screen when mystery(13) called

Screen 4 :Screen when mystery(297) called

Screen 5 :Screen when mystery(-3456) called

Below table shows details

Call

Value Returned

mystery(0)

0

mystery(5)

4

mystery(13)

86

mystery(297)

702

mystery(-3456)

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
Recursive Tracing. For each call to the following method, indicate what value is returned: public static...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT