Here's a sample recursive method for Fibonacci numbers:
public int fibonacci(int n)
{
if(n == 1 || n == 2) // The reason why I did it this way was because I made the recursive call two different ways so I made it
//clear with the ending statement that there was two different ways this was being called
{
return 1;
}
else
{
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
Here's a sample recursive algorithms to find a N x N board that has N queens on it where no of the queens can attack each other.
public void nQueens(int i)
{
if (i == n)
{
return solution;
}
else
{
for (int k = 0; k < n; k++)
{
col[i] = k;
if (isPromising(i))
{
nQueens(i+1);
}
}
}
}
private boolean isPromising(int n)
{
for (int i = 0; i < n; i++)
{
if (col[i] == col[n])
return false;
if (Math.abs((col[i] - col[n])) == (n - i))
return false;
}
return true;
}