Turning Recursion Method into Iterative method
Hello, I'm relatively new to java and I am sort of stuck on how exactly I would turn a previously defined recursive method into another method only using iteration.
Here is my current code:
public class RecursiveLab
public static void main(String  args)
Scanner scan = new Scanner(System.in);
System.out.print("Please enter an integer: ");
int userChoice = scan.nextInt();
int result = mystery(userChoice);
System.out.println("The result was: " + result);
public static int mystery(int num)
if(num <= 2)
return mystery(num - 1) + mystery(num - 2);
public static int mystery2(int num)
int sum = 0;
Mystery is the recursive and mystery2 is going to be the iterative. Don't worry about the main not calling it, that will come later. I'm just looking at a new version for my mystery method.
Anyone have any good ideas to accomplish this?
Moderator Edit: Code tags added