Write a java program to find the sum of the 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 the 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.
- The 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]
Program to display Fibonacci series in Java
Before going to find the sum of the Fibonacci series, first, we will display the Fibonacci series using Java:
- Input a number.
- Pass the number to the fibonacciSeries() method.
- Inside the method, declare and initialize variables a,b, and c with 0,1, and 0.
- Now, use the for loop and add the first two number of the sequence and store it in c. Repeat this process till the range is covered and print value of c.
package com.date; import java.util.Scanner; public class FibonacciSeries { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); fibonacciSeries(n); } private static void fibonacciSeries(int n) { int a = 0, b = 1, c = 0; System.out.print(a + " " + b + " "); for (int i = 2; i < n; i++) { c = a + b; System.out.print(c + " "); a = b; b = c; } } }
Output
Java program to find the sum of Fibonacci series
Following are the steps to find the sum of the Fibonacci series in Java:
- Input the number.
- Pass the number to the sumOfFibonacci() method.
- Inside the method, declare and initialize four variables i.e a,b,c, and d.(where a and b are initialized to 0 and 1).
- Now, use the for loop and add the first two number of the sequence and store it in c. Then, just find each subsequent number by adding the previous number. And, store the sum in d.
- At last, return the value of d.
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; } }
Output
Sum of Fibonacci series in Java using recursion
Following are the steps to quickly find the sum of the Fibonacci series using recursion:
- Input the number.
- Pass the number to the sumOfFibonacci() method.
- Inside the method, check for the following condition
- If the value is less than 0 return 0.
- If the value is 1 then return 1.
- Otherwise, recursively call for the previous two values.
- At last, for each recurve call return recursive method.
package com.demo2; 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-1)); } public static int sumOfFibonacci(int n) { if(n <= 0) return 0; if(n == 1) return 1; return sumOfFibonacci(n-1)+sumOfFibonacci(n-2) + 1; } }
Output
Thus, in this way, we learn how to find the sum of the Fibonacci number in java.