Showing posts with label Python Programming. Show all posts
Showing posts with label Python Programming. Show all posts
Python Code Example - Fibonacci Series in Python
Today we'll show how to get the Fibonacci number using Python Programming language. Fibonacci Series is on e of the most common data structure in Computer Science. We;ll just do it using Python programming language.
Code Part
n_string = input('How many fibonacci number do want to get = ')
n = int(n_string)
fib0 = 0
fib1 = 1
fib = 0
i = 0
while i < n:
fib = fib0+fib1
print(fib)
fib0 = fib1
fib1 = fib
i = i+1
Logic behind the Fibonacci Series
Two fixed Fibonacci numbers are = 0 1
And next will be generated from this.
0
1
0 + 1 = 1
1 + 1 = 2
2 + 1 = 3
3 + 2 = 5
5 + 3 = 8
....
That means the Fibonacci number will be first_number + second number.
Then, the first number will be second number and the second number will be Fibonacci number.
Code Explanation Line By Line
n_string = input('How many gibonacci number do want to get = ')
n = int(n_string)
In first two line, take input from user by input() method. Which returns a string. Then in second line just convert it to integer using int()
Now Define the first two fibonacci number and variables
fib0 = 0
fib1 = 1
fib = 0
i = 0
Now start loop of while until total number of taken from user reaches. and then just do what I've said in logic part.
That means the Fibonacci number will be first_number + second number.
Then, the first number will be second number and the second number will be Fibonacci number.
while i < n:
fib = fib0+fib1
print(fib)
fib0 = fib1
fib1 = fib
i = i+1
Full Code + Output(pic)
Having any problem, just comment out here
Subscribe to:
Comments
(
Atom
)


