I have the following code which provides the correct values for n < 47.
public static int fib(int n) { int nthTerm = 0; if (n == 2) nthTerm =
If you use long, you support perfectly the range over 1000; but if you want support all possible value then you need use BigInteger.
long
BigInteger
A example use long:
public static long fib(int n) { long f0 = 1; long f1 = 1; long c = 2; while(c < n) { long tmp = f0+f1; f0 = f1; f1 = tmp; c++; } return f1; }