Fibonacci series is a series that you get after finding the Fibonacci numbers, Fibonacci numbers are the numbers that you get Generated by adding the two previous terms. Initial terms are 0 and 1 as shown below.
fib(0) = 0
fib(1) = 1
fib(n) = fib(n-1) + fib(n-2)
Where n is a positive integer. refer the below output section for Fibonacci series examples
Write a Python Program to Print Fibonacci Series up to n Terms
In the main program that take a variable named as and and using the input function call ask a user to enter the value of n. using the and value you have to generate the Fibonacci series up to n terms. convert the value n from string to an integer.
n = int(input("Enter Value of n : "))
Using the print function cal print a user-friendly message that is Fibonacci series up to n terms are etc. Take two integer variables “a and b” and initialize them to 0 and 1 these are the basic conditions for Fibonacci series that is a starting point from 0 and 1
print(f"Fibonacci Series up to {n} Terms is ...")
a = 0
b = 1
Using the for loop, iterate all the numbers from 0 to N and print the value of a Within the for loop. Once the value of a is printed then swap the values of A and B will be set to b; and b value will be set to a + b.
for _ in range(n):
print(a, end=" ")
a, b = b, a+b
This will print out the series up to n terms by swapping the values of a and b continuously up to n terms.
Complete Python program
============================
n = int(input("Enter Value of n : "))
print(f"Fibonacci Series up to {n} Terms is ...")
a = 0
b = 1
for _ in range(n):
print(a, end=" ")
a, b = b, a+b
Output
====================
Enter Value of n : 12
Fibonacci Series up to 12 Terms is ...
0 1 1 2 3 5 8 13 21 34 55 89
Output
=====================
Enter Value of n : 20
Fibonacci Series up to 20 Terms is ...
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
Output
=====================
Enter Value of n : 30
Fibonacci Series up to 30 Terms is ...
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229
Conclusion
=================
Execute the program by yourself and provide the random integer value to it and note down the result.
Comment down below if you have any suggestions for the above program.
0 Comments