I want to change this recursive method into an iterative method using a while and or for loop but am having trouble. Here is the Recursive method I want to translate:
public static int recursiveNum (int n)
{
if(n<=2)
{
return 1;
}
else
{
return recursiveNum(n-1)+recursiveNum(n-2);
}
}
What i've attempted so far:
public static int iterativeNum (int n) {
while (i<n) {
i++;
if (n<=2) {
return 1;
}
else {
return n = (n-1) + (n-2);
}
}
return n;
}