Python Program to find fibonacci series
Program
num = int(input("Enter the range of fibonacci series:\t"))
first = 0
second = 1
i = 0
print("Fibonacci Series")
while(i < num):
if(i <= 1):
next = i
else:
next = first + second
first = second
second = next
print(next,"\t",end="")
i = i + 1
print()
This program generates the Fibonacci series up to a specified range entered by the user.
-
User Input for Range
- The user specifies how many terms of the Fibonacci series should be printed.
-
Initialize Variables
first = 0
andsecond = 1
store the first two numbers of the series.
-
Loop to Generate Fibonacci Series
- If
i <= 1
, the next term isi
(handling the first two values separately). - For other terms, the next number is calculated as
first + second
. - The variables
first
andsecond
are updated accordingly.
- If
-
Printing the Series
- The
print()
function prints each term in a single line with a tab space.
- The
Key Takeaways
- Uses a while loop to generate Fibonacci numbers up to the given range.
- The first two numbers are handled separately.
- Updates variables dynamically to compute new terms efficiently.
Output
Enter the range of fibonacci series: 6
Fibonacci Series
0 1 1 2 3 5