September 29, 2018

# Write a java program to find the sum of fibonacci series

Write a java program to find the sum of fibonacci series. Write a program to read an integer n, generate fibonacci series and calculate the sum of first n numbers in the series. Print the sum.

**Note: **First two numbers in a Fibonacci series are 0, 1 and all other subsequent numbers are sum of its previous two numbers. Example – 0, 1, 1, 2, 3, 5…

**Input and Output Format:**

Input consists of an integer, which corresponds to n.

Output consists of an Integer (sum of fibonacci numbers).

Refer sample output for formatting specifications.

**Sample Input :**

5

**Sample Output :**

7

**[0 + 1 + 1 + 2 + 3 = 7]**

## java program to find the sum of fibonacci series

package com.demo; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.println(sumOfFibonacci(n)); } public static int sumOfFibonacci(int n) { int a = 0, b = 1, c = 0, d = 0; for (int i = 3; i <= n; i++) { c = a + b; b = c; a = b; d = d + c; } return d; } }