In this program, we will print the Fibonacci series up to n terms.
Steps to solve the program
- Take a number as input up to which you want to print the Fibonacci series.
- Create two variables num1, and num2 and assign their values equal to 0 and 1.
- Create a count variable and assign its values equal to 0.
- While the count variable is less than the input term print num1.
- Add num1 and num2 and store the result in a new variable n2.
- Change num1 to num2 and num2 to n2 and add 1 to the count variable.
- Print the output.
num = int(input("Enter the number= "))
count = 0
num1,num2 = 0,1
print("Sequence is: ",end=" ")
while count < num:
print(num1,end=" ")
n2 = num1 + num2
num1 = num2
num2 = n2
count += 1
Output :
Enter the number= 15
Sequence is: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377