Home > Algorithms > Generate nth Fibonacci number

Generate nth Fibonacci number

July 7, 2012

public static int getNthFibonacci(int n)
{
if(n==1)return 0;
if(n==2)return 1;
int a=0;
int b=1;
for (int i = 0; i < n-2 ; i++)
{
b = a+b;
a = b -a;
// System.out.println(b);
}
return b;
}
Advertisements
Categories: Algorithms
%d bloggers like this: