codinasion-archive / codinasion-programme

An open source codebase for sharing programming solutions.
https://codinasion.vercel.app/programme
MIT License
62 stars 147 forks source link

Fibonacci series using recursion #73

Closed harshraj8843 closed 2 years ago

harshraj8843 commented 2 years ago

Title of article

Write a Python program to print Fibonacci series.

Additional information

Input  : 10
Output : 0 1 1 2 3 5 8 13 21 34

Code of Conduct

pa11teddu commented 2 years ago

Fibonacci Series

Takes the user input Initial Values for Fibonacci Series are 0 and 1 Use for loop till n-2 Add the n1 and n2 Swap the values Print sum

n = int(input())
n1 = 0
n2 = 1
print(n1,n2,end=" ")
for i in range(0,n-2):
    sum = n1+n2
    n1 = n2
    n2 = sum
    print(sum, end=" ")
pa11teddu commented 2 years ago

This is the more optimized one

n = int(input())
n1, n2 = 0, 1
for i in range(0,n):
print(n1, end=" ")
n1, n2  = n2, n1+n2
harshraj8843 commented 2 years ago

Hii @PA11TEDDU

Thanks for contributing to this open source project 😃

Could you open a pull request for this