KIN
0
Q:

fibonacci series in python

# Program to display the Fibonacci sequence forever

def fibonacci(i,j):
  print(i;fibonacci(j,i+j))
fibonacci(1,1)
1
# # this is the fibonacci series by KV for for loop :)

n = int(input(":"))
for i in range(1,n+1):
    print(i, end = "")
0
# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms
n1, n2 = 0, 1
count = 0

# check if the number of terms is valid
if nterms <= 0:
   print("Please enter a positive integer")
elif nterms == 1:
   print("Fibonacci sequence upto",nterms,":")
   print(n1)
else:
   print("Fibonacci sequence:")
   while count < nterms:
       print(n1)
       nth = n1 + n2
       # update values
       n1 = n2
       n2 = nth
       count += 1
5
#Recursive Solutions
def fibo(n):
  if n<=1: return 1
  return fibo(n-1)+fibo(n-2)

fibo(5)
#OUTPUT:
#120
0
def fib(n, a=0, b=1): 
    '''fib(n) -> int
    returns the nth Fibonacci number
    '''
    if (n == 0): 
        return a
    return fib(n - 1, b, a + b)
-1
nterms = int(input("How many terms? "))

# first two terms
n1, n2 = 0, 1
count = 0

# check if the number of terms is valid
if nterms <= 0:
   print("Please enter a positive integer")
elif nterms == 1:
   print("Fibonacci sequence upto",nterms,":")
   print(n1)
else:
   print("Fibonacci sequence:")
   while count < nterms:
       print(n1)
       nth = n1 + n2
       # update values
       n1 = n2
       n2 = nth
       count += 1
        
0

New to Communities?

Join the community