Swift Program to Display Fibonacci Series


Fibonacci sequence is a sequence of numbers in which every number is the sum of the preceding two numbers and the starting number of this sequence are 0 and 1. So the general Fibonacci series is −

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

Formula

Following is the formula of Fibonacci series −

Fn = Fn-1 + Fn-2

Below is a demonstration of the same −

Suppose we enter the following input −

Number = 5

Following is the desired output −

Fibonacci Series is- 0, 1, 1, 2, 3

We can find the Fibonacci series using any of the following methods −

  • Iterative method

  • Recursion method

Iterative Method

We can create a Fibonacci series using the iterative method. In this method, we use loops to find the series.

Algorithm

The algorithm is explained below −

  • Step 1 − Create a function.

  • Step 2 − Declare three variables with values - n1 = 0, n2 = 1, and nR = 0.

  • Step 3 − Run a for loop from range 0 to <num and find the fibonacci series by adding the preceding numbers.

  • Step 4 − Declare a variable with value - val = 10

  • Step 5 − Call the function with one argument and display the final output.

Example

The following program shows how to count the number of digits in an integer using the iterative method.

import Foundation import Glibc func fibonacciSeries(num: Int) -> Int{ // The value of 0th and 1st number of the fibonacci series are 0 and 1 var n1 = 0 var n2 = 1 // To store the result var nR = 0 // Adding two previous numbers to find ith number of the series for _ in 0..<num{ nR = n1 n1 = n2 n2 = nR + n2 } return n1 } var val = 10 print("Following is the Fibonacci series:") // Iterate for every number stating from 0 to val for j in 0...val{ let output = fibonacciSeries(num: j) print(output) }

Output

Following is the Fibonacci series:
0
1
1
2
3
5
8
13
21
34

Here in the above code, we create a function named fibonacciSeries(), which takes one argument and return the fibonacci series. This function uses for loop to calculate the sum of the preceding two numbers to find the next number using the following code −

for _ in 0..<num{
   nR = n1 
   n1 = n2
   n2 = nR + n2
}

Outside the function we create a variable with value named val = 10. Now we use a for loop to display the fibonacci series. This loop starts from 0 to 10, and inside this loop, we call fibonacciSeries() function with an argument which means we call fibonacciSeries() function 10 times to find the series starting from 0 to 10.

Recursive Method

We can create fibonacci series using the recursive method. In this method, we create a recursive function which call itself to find the fibonacci series starting from 1 to till the specified number.

Algorithm

The algorithm is explained below −

  • Step 1 − Create a function.

  • Step 2 − Use if-else statement to check if the given number is 1 or 0 if the given number is 1 then return 1. If the given number is 0 return 0.

  • Step 3 − Return Fibonacci series by calling the function itself.

    return fibonacciSeries(num: num - 1) + fibonacciSeries(num: num - 2)

  • Step 4 − Declare a variable with value - val = 10

  • Step 5 − Run a for loop from range 0 to val

  • Step 6 − Call the function with one argument and display the final output.

Example

The following program shows how to count the number of digits in an integer using recursive method.

import Foundation import Glibc func fibonacciSeries(num: Int) -> Int{ // Checking number for num 0 and 1 // Or we can say it is the base condition for recursive // function if (num == 0){ return 0 } else if (num == 1){ return 1 } // To find the with number we add the previous two numbers // Here we find the previous two numbers by calling the function itself and return the result. return fibonacciSeries(num: num - 1) + fibonacciSeries(num: num - 2) } var val = 10 print("Following is the Fibonacci series:") // Iterate for every number stating from 0 to val for j in 0...val{ let output = fibonacciSeries(num: j) print(output) }

Output

Following is the Fibonacci series:
0
1
1
2
3
5
8
13
21
34
55

Here, in the above code we create a recursive function named as fibonacciSeries(). It takes one argument and returns fibonacci series. Now outside the function, we create a variable with value named as val = 10. To display the fibonacci series starting from 0 to 10 we use a for loop, this loop run from 0 to 10 and inside this loop we call the fibonacciSeries() function with an argument and this function return fibonacci series starting from 0 to 10. So the working of the fibonacciSeries() function is −

1st call of fibonacciSeries() with 0: fibonacciSeries(0) = return 0
2nd call of fibonacciSeries() with 1: fibonacciSeries(1) = return 1
3rd call of fibonacciSeries() with 3: fibonacciSeries(2) =
return fibonacciSeries(num: 2 - 1) + fibonacciSeries(num: 2 -  2) = fibonacciSeries(num:1) + fibonacciSeries(num: 0) = 1 + 0 = 1
return 1
4th call of fibonacciSeries() with 4: fibonacciSeries(4) =
return fibonacciSeries(num: 4 - 1) + fibonacciSeries(num: 4 -  2) = fibonacciSeries(num:3) + fibonacciSeries(num: 2) = 1 + 1 = 2
return 2
...
10th call of fibonacciSeries() with 10: fibonacciSeries(10) =
return fibonacciSeries(num: 10 - 1) + fibonacciSeries(num: 10 -  2) = fibonacciSeries(num:9) + fibonacciSeries(num: 8) = 21 + 34 = 55
return 55

Updated on: 05-Aug-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements