How to find the sum of 0, 1, 1, 2, 3, 5, 8.. or Fibonacci series in Java:
In this post, we will learn how to find the sum of the Fibonacci series upto a specific distance. The Fibonacci series or Fibonacci sequence is a series where each number is equal to the sum of two previous numbers in the series. The first two numbers of Fibonacci series are 0 and 1.
For example, 0, 1, 1, 2, 3, 5, 8… is a Fibonacci series.
We will write one Java program that will find the sum of this series upto nth value. This program will take the value of n as input from the user and it will print the sum.
For example, if n is 5, then the sum will be 0+1+1+2+3 = 7.
Algorithm to print the sum of Fibonacci series:
Our program will use the below algorithm to find the sum:
- Get the value of n from the user.
- Initialize two variables to store the current value and previous value of the Fibonacci series. Assign the current value as 1 and previous value as 0.
- If the value of n is 0, return 0, if it is 1, return 1. If it is more than 1, use one loop to find the sum.
- Create one sum variable and initialize it as 0. Using a loop, find the sum of all values to n and store that value in this variable.
- Print the sum variable.
Java program to get the sum of Fibonacci series up to nth value:
Below is the complete Java program:
import java.util.Scanner;
class Main {
private static int findSum(int n) {
//3
int currentValue = 1, prevValue = 0, sum = 0, temp;
//4
if (n <= 0)
return 0;
if (n == 1)
return 1;
//5
sum = 1;
for (int i = 2; i < n; i++) {
temp = currentValue;
currentValue = prevValue + currentValue;
prevValue = temp;
sum += currentValue;
}
return sum;
}
public static void main(String[] args) {
//1
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n: ");
n = sc.nextInt();
//2
System.out.println("Sum = " + findSum(n));
}
}
Explanation:
The commented numbers in the above program denote the step numbers below:
- Create one variable n to store the value of n. sc is a Scanner variable to read user inputs. Ask the user and read the value of n.
- Call findSum and print the sum. findSum takes the value of n as a parameter and returns the sum.
- In findSum, create four integer variables, currentValue to store the current value of the series, prevValue to store the previous value of the series, sum to hold the sum and temp to use as temporary storage.
- If the value of n is less than or equal to 0, return 0. If the value of n is equal to 1, return 1.
- Initialize the value of sum as 1. Run one loop from i = 2 to i = n - 1. We already have the values of the first and the second values of the series stored in the variables. So, we are running the loop from the third element. Inside the loop, assign currentValue to temp, update currentValue as the sum of previous two values and assign the value of temp to prevValue. Add the value of currentValue to sum. Since sum is initialized as 1, it was holding the sum of the first two numbers before the loop starts. We are adding the values of the Fibonacci series starting from i = 2 inside the loop. Return sum once the loop ends.
Sample output:
If you run this program, it will print output as like below:
Enter the value of n:
7
Sum = 20
Enter the value of n:
0
Sum = 0
Enter the value of n:
1
Sum = 1
Java program to get the sum of Fibonacci series using a while loop:
We can also use any other loop to find the sum up to nth value. For example, the below program uses a while loop to find the sum.
import java.util.Scanner;
class Main {
private static int findSum(int n) {
int currentValue = 1, prevValue = 0, sum = 0, temp;
if (n <= 0)
return 0;
if (n == 1)
return 1;
sum = 1;
int i = 2;
while (i < n) {
temp = currentValue;
currentValue = prevValue + currentValue;
prevValue = temp;
sum += currentValue;
i++;
}
return sum;
}
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n: ");
n = sc.nextInt();
System.out.println("Sum = " + findSum(n));
}
}
It is exactly same as the above program. The only difference is that we are using a while loop instead of for. It initializes i as 2 before the loop starts and works in a similar way.
If you run this program, it will give similar output.
Recursive Java program to find the Fibonacci series sum:
We can also find this sum recursively. A recursive program calls itself again and again until it finds the result.
Let’s take a look at the below program:
import java.util.Scanner;
class Main {
private static int findSum(int currentValue, int prevValue, int i, int n) {
if (n <= 0)
return 0;
if (n == 1)
return 1;
if (i == n)
return 0;
return currentValue + findSum(currentValue + prevValue, currentValue, i + 1, n);
}
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n: ");
n = sc.nextInt();
System.out.println("Sum = " + findSum(1, 0, 1, n));
}
}
- findSum is a recursive function. It calculates the sum of the first nth numbers of a Fibonacci series.
- findSum takes the current value, previous value, i i.e. the current index and n.
- It returns 0 if the value of n is 0, 1 if the value of n is 1 and 0 if the value of i is equal to n. Else, it add the currentValue with thre result of findSum, i.e. it calls findSum again.
- On the next call, it updates the current value as the sum of current and previous values, and currentValue as the prevValue.
If you run this program, it will give a similar result as the above examples.
You might also like:
- Java HashMap.merge() method explanation with examples
- Java program to convert a boolean array to string array
- 5 Different ways to append text to a file in Java
- Can Enum implements interfaces in Java
- Java program to find special numbers in a range
- Create a Rectangle class in Java and calculate area, perimeter
- Java program to check if a number is special or not