
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Even Sum of Fibonacci Series Till Number N
In this article, we will learn how to find the even sum of the Fibonacci series up to a given number N. A Fibonacci series is a series where the next term is the sum of the previous two terms. An even Fibonacci series is a group of all the even numbers in the Fibonacci series.
The first two terms of the Fibonacci sequence are 0 and 1.
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 the Fibonacci series. Suppose our input is -
Suppose our input is: Value of n is: 10 The output would be: Even sum of Fibonacci series is 10945
Let's see the following approaches to find the even sum of the Fibonacci series -
Approach 1
In this approach, the Fibonacci series (next = a + b) generates numbers up to N, checking if each number is even (b%2 == 0). It adds only the even Fibonacci numbers to the sum, through iteration and condition checking.
Algorithm
Step 1- Start Step 2- Declare an integer variable N and assign a value (e.g., N = 100) Step 3- Declare and initialize three integer variables a = 0, b = 1, sum = 0 Step 4- Use a while loop to get fibonacci sequence.. Step 5- Inside the loop, check if b is divisible by 2 (b % 2 == 0). Step 6- If yes, add b to sum and print b. Step 7- initialize integer variable next to store Fibonacci sequence. (next = a + b). Step 8- Assign the value of b to a. Step 9- Assign the value of next to b. Step 10- After loop ends, when b exceeds N. Step 11- Stop
Example
Let's see the following program to understand the above approach -
public class EvenFibonacciSum { public static void main(String[] args) { int N = 100; // You can change N to any positive number int a = 0, b = 1; int sum = 0; System.out.println("Even Fibonacci numbers up to " + N + " are:"); while (b <= N) { if (b % 2 == 0) { sum += b; System.out.print(b + " "); } int next = a + b; a = b; b = next; } System.out.println("\nSum of even Fibonacci numbers = " + sum); } }
On compiling, the above program gives you the following output.
Even Fibonacci numbers up to 100 are: 2 8 34 Sum of even Fibonacci numbers = 44
Approach 2
In this solution, we store Fibonacci numbers up to index 2*N in an array. It adds only the even-position Fibonacci numbers (based on the index) to get the final sum.
Algorithm
Step1- Start Step 2- Declare three integers my_input, j and sum. Step 3- Assign a value to my_input (e.g., 10). Step 4- Declare an integer array with size. Step 5- Initialize the first two Fibonacci numbers 0 and 1. Step 6- Initialize sum = 0. Step 7- 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 8- Display the result. Step 9- Stop
Example
Let's see the following program to understand the above approach -
public class FabonacciSum { public static void main(String[] args){ int my_input, j, sum; my_input = 10; 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); } }
On compiling, the above program gives you the following output.
The even sum of fibonacci series till number 10 is 10945
Example
Here, the input is being entered by the user based on a prompt. You can try this example live in our coding 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