Fibonacci Series in Swift

Fibonacci Series using for loop in Swift. Fibonacci is a series of numbers in which each number is the sum of the two preceding numbers. This program prints the first 10 numbers in the Fibonacci Series.

Source code :


var x = -1 ,y = 1, sum = 0;
    
for i in 0..<10 {

sum = x+y;
x   = y;
y   = sum;

print(sum);
	
}

Output :

0
1
1
2
3
5
8
13
21
34

Notes :

Initially assign -1 to x and 1 to y.

Now iterate from 0 to 10.

Add the x and y values and assign it to the sum

Replace the value of x by y and y by sum. Hence the Fibonacci Series can be done with the help of for loop.


Output :


                

Comments :