java nth fibonacci number

Solutions on MaxInterview for java nth fibonacci number by the best coders in the world

showing results for - "java nth fibonacci number"
Ishaan
29 Jul 2017
1// Using following formula you can find nth Fibonacci Number without using any loops;
2// Created by using resources from " https://math.hmc.edu/funfacts/fibonacci-number-formula/ "
3
4class Find
5{
6    public int nFibo(int n)
7    {
8        double Phi=((1+Math.sqrt(5))/2);
9        double phi=((1-Math.sqrt(5))/2);
10        
11        int ans=(int)((Math.pow(Phi,n)-(Math.pow(phi,n)))/Math.sqrt(5));
12        
13        return ans;
14    }
15}
Alexander
13 Jul 2020
1for (loop = 0; loop < n; loop ++)
2{
3    fibonacci = num + num2;
4    num = num2;
5    num2 = fibonacci;
6}
7System.out.print(num);
8