Before writing code for Fibonacci sequence let's know what is Fibonacci number.
The fibonacci sequence looks like 0 1 1 2 3 5 8 13 21 ....
The Fibonacci sequence is a set of numbers that starts with zero followed by one.
And each number is equal to the sum of the preceding two numbers.
Example:
Step 1:
1st number=0
2nd number=1
Here term is 2
Then next number will be sum of 1st number & 2nd number. That is 1
Step 2:
Now the sequence became
0 1 1
Here term is 3
Next number = 1 + 1 = 2
Step 3:
New sequence is 0 1 1 2
Here term is 4
Next number = 1 +2 = 3
So new sequence became 0 1 1 2 3
As you can see every number is equal to sum of 2 previous number
#take input from the user
term=int(input("Enter term: "))
num1 = 0num2 = 1count = 0while count < term: print(num1) nth = num1 + num2 num1 = num2 num2 = nth count += 1
Output 1:
enter term : 5
0
1
1
2
3
Output 2:
enter term :10
0
1
1
2
3
5
8
13
21
34

Thanks a lot.
ReplyDelete