Swift Program to Find Sum of Even Fibonacci Terms Till number N


This tutorial will discuss how to write a Swift program to find sum of even fibonacci terms till number N.

A series of numbers in which every number is the sum of the two preceding numbers is known as Fibonacci series. The starting number of the Fibonacci series is 0 and 1, so the series is −

0, 1, 1, 2, 3, 5, 8, 13, 21, ……

A number that is multiple of two is known as the even number. Or in other words, a number that is completely divisible by two is known as even number. For example, 2, 4, 6, 8, etc.

So we find the sum of even fibonacci terms till number N.

Below is a demonstration of the same -

Input

Suppose our given input is

Enter the number - 10

Output

The desired output would be −

Sum of even Fibonacci terms are 10

Here the output is 10 because the even numbers present from 0 to 10 in fibonacci series are 2 and 8 so 2+8 = 10.

Algorithm

Following is the algorithm −

  • Step 1 − Create a function.

  • Step 2 − Declare four variables with values name as “temp”, “sum”, “n1”, “n2”. Here sum is used to store the sum of even terms, n1 and n2 are the initial two value of fibonacci series.

  • Step 3 − Run a while loop till n2 <num.

  • Step 4 − use if statement to check for even numbers and find their sum.

if (n2 % 2 == 0) { 
   sum += n2 
}
  • Step 5 − Adding two previous numbers to find ith number of the series.

  • Step 6 − Return the sum of even numbers.

  • Step 7 − Call the function with argument and print output.

Example 1

The following program shows how to find the sum of even fibonacci terms till number N.

import Foundation import Glibc // Function to find the sum of even fibonacci terms func SumOfEvenFibonacci(num: Int) -> Int{ var temp = 0 // Store the sum of even numbers var sum = 0 // Initial two values of fibonacci sequence var n1 = 0 var n2 = 1 while(n2 < num){ // Checking for even numbers // By calculating their remainder if the remainder is 0 // then the number is number is even if not // then the number is not even if (n2 % 2 == 0){ sum += n2 } // Adding two previous numbers to find ith // number of the series temp = n1 n1 = n2 n2 += temp } return sum } print("Sum of even fibonacci terms:", SumOfEvenFibonacci(num: 100))

Output

Sum of even fibonacci terms: 44

Here, in the above code, we create a function named SumOfEvenFibonacci() to find the sum of even fibonacci terms. In this function, we find the fibonacci ser ies by adding two previous numbers and then check for all the numbers of fibonacci series starting from 0 to 100 for even numbers and then find the sum of even numbers using the following code:

while(n2 < num){
   if (n2 % 2 == 0){
      sum += n2
   }
   temp = n1
   n1 = n2
   n2 += temp
}
return sum

Working of the above code is :

temp = 0
sum = 0
n1 = 0
n2 = 1
num = 100
while(1 < 100){
   if (1 % 2 == 0) => (1 == 0) = condition false{
      sum += n2
   }
   temp = 0
   n1 = 1
   n2 = 1 + 0 = 1
}
while(1 < 100){
   if (1 % 2 == 0) => (1 == 0) = condition false{
      sum += n2
   }
   temp = 1
   n1 = 1
   n2 = 1 + 1 = 2
}
while(2 < 100){
   if (2 % 2 == 0) => (0 == 0) = condition true{
      sum = sum + n2 => sum = 0 + 2 = 2
   }
   temp = 1
   n1 = 2
   n2 = 2 + 1 = 3
}
... so on till num = 100.

Now we call the function and pass 100 as an argument and display the sum that is 44(2+8+34 = 44).

Example 2

The following program shows how to find the sum of even fibonacci terms till number N.

import Foundation import Glibc // Function to find the sum of even fibonacci terms func SumOfEvenFibonacci(num: Int) -> Int{ var temp = 0 var sum = 0 var n1 = 0 var n2 = 1 while(n2 < num){ // Checking for even numbers if (n2 % 2 == 0){ sum += n2 } // Adding two previous numbers to find ith // number of the series temp = n1 n1 = n2 n2 += temp } return sum } // Taking input from the user print("Please enter the value:") var val = Int(readLine()!)! // Calling function var res = SumOfEvenFibonacci(num: val) print("Sum of even fibonacci terms are:", res)

STDIN Input

Please enter the value:
10000

Output

Sum of even fibonacci terms are: 3382

Here, in the above code, we creat e a function named SumOfEvenFibonacci() to find the sum of even fibonacci terms. Here we take the input from the user and pass this input in the SumOfEvenFibonacci() function as an argument. Suppose user enter the number 10000 so the total sum of even numb ers present from 0 to 10000 is 3382.

Updated on: 19-Aug-2022

787 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements