Sum of Fibonacci Numbers | Set 2 | Java program for the above approach ; Function to find the sum of first N + 1 fibonacci numbers ; Apply the formula ; Print the result ; Driver Code ; Function Call
import java . io . * ; class GFG { public static void sumFib ( int N ) { double num = ( 1 - Math . sqrt ( 5 ) ) / 2 ; long val = Math . round ( Math . abs ( 1 / ( Math . pow ( num , N + 2 ) + Math . pow ( num , N + 1 ) + Math . pow ( num , N ) + Math . pow ( num , N - 1 ) ) ) - 1 ) ; System . out . println ( val ) ; } public static void main ( String [ ] args ) { int N = 3 ; sumFib ( N ) ; } }