# Java Program to Find Even Sum of Fibonacci Series till number N

In this article, we will understand how to find even sum of Fibonacci Series till number N. A Fibonacci series is sequence of numbers formed by the sum of its two previous integers. An even Fibonacci series is all the even numbers of the Fibonacci series.

Fibonacci series generates the subsequent number by adding two previous numbers. Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken 0, 1 or 1, 1 respectively.

Fn = Fn-1 + Fn-2

Hence, a Fibonacci series can look like this −

F8 = 0 1 1 2 3 5 8 13


or, this,

F8 = 1 1 2 3 5 8 13 21

Below is a demonstration of the even sum of Fibonacci series −

Input

Suppose our input is −

Value of n is: 10


Output

The desired output would be −

Even sum of Fibonacci series is 10945

## Algorithm

Step1- Start
Step 2- Declare three integers my_input, i, sum
Step 3- Prompt the user to enter two integer value/ Hardcode the integer
Step 5- Use a for loop to iterate through the integers from 1 to N and assign the sum of
consequent two numbers as the current Fibonacci number.
Step 6- Display the result
Step 7- Stop

## Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .

import java.util.Scanner;
import java.io.*;
public class FabonacciSum {
public static void main(String[] args){
int my_input, i, sum;
System.out.println("Required packages have been imported");
Scanner my_scanner = new Scanner(System.in);
System.out.println("A reader object has been defined ");
System.out.println("Enter the value of N: ");
my_input = my_scanner.nextInt();
int fabonacci[] = new int[2 * my_input + 1];
fabonacci[0] = 0;
fabonacci[1] = 1;
sum = 0;
for (i = 2; i <= 2 * my_input; i++) {
fabonacci[i] = fabonacci[i - 1] + fabonacci[i - 2];
if (i % 2 == 0)
sum += fabonacci[i];
}
System.out.printf("Even sum of fibonacci series till number %d is %d" , my_input, sum);
}
}

## Output

Required packages have been imported
A reader object has been defined
Enter the value of N:
10
Even sum of fibonacci series till number 10 is 10945

## Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

import java.util.Scanner;
import java.io.*;
public class FabonacciSum {
public static void main(String[] args){
int my_input, j, sum;
my_input = 10;
System.out.println("The value of N: ");
int fabonacci[] = new int[2 * my_input + 1];
fabonacci[0] = 0;
fabonacci[1] = 1;
sum = 0;
for (j = 2; j <= 2 * my_input; j++) {
fabonacci[j] = fabonacci[j - 1] + fabonacci[j - 2];
if (j % 2 == 0)
sum += fabonacci[j];
}
System.out.printf("The even sum of fibonacci series till number %d is %d" , my_input, sum);
}
}

## Output

The value of N:
The even sum of fibonacci series till number 10 is 10945

Updated on: 21-Feb-2022

2K+ Views