Learn how to write a program to print fibonacci series in python. Generate different types of fibonacci series and sequences in Python.
Print fibonacci series in python
Python programs and source codes:
Q1. Write a program that takes an input from the user and generates the Fibonacci series till user-defined number using
a) Function, named "fibonacci"Solution:
.Source Code
def fib(n):if n == 0 :return 0elif n == 1 or n == 2:return 1return fib(n-2)+fib(n-1)n = int(input("number: "))list1=[]for i in range(n):list1.append(fib(i))print(list1)Q2. Write a program that takes input from the user and find all the prime number from 0 to user-defined numberr, usinga) function, named "primeNume"
Solution:
.Source Code
def checkprime(a):
if a==1 or a==0:
return False
else:
i = 2
while i < a:
if a%i==0:
return False
break
else:
i+=1
return True
def checkprimerecursion(a , i=2):
if a < 2:
return False
elif a==2:
return True
if a%i==0:
return False
if i*i > a:
return True
return checkprimerecursion(a , i+1)
#prime no checker w/o recursion
a=int(input("Enter first no: "))
b=int(input("enter second no: "))
list1=[i for i in range (a,b+1)]
for i in list1:
if checkprime(i)==False:
list1.remove(i)
print("(w/o recursion)The set of prime numbers from",a,"to",b,"inclusive are ",list1)
for i in list1:
if checkprimerecursion(i)==False:
list1.remove(i)
print("(recursion)The set of prime numbers from",a,"to",b,"inclusive are ",list1)
Q3. Write a program that takes input from the user and find the sum of all number from 0 to the user-defined number using
a) function, named "numberSum"Solution:
.Source Code
def numberSum(n):
total=0
for i in range(n+1):
total=total+i
return total
def numberSumrecursive (n):
if n==0:
return 0
return n+numberSumrecursive(n-1)
print(numberSumrecursive(5))
print (numberSum(5))
Q4:Write a program that takes input from user and find the all possible devisor(s) of a given number and store and display the answer, using
a) Function, named "divisors"Solution:
.Source Code
def devisor(a):
lst = [i for i in range(1,a+1) if a%i==0]
return lst
x = int(input("Enter the value you want: "))
print(devisor(x))
def divisor_recursive(n,i=1):
if i <= n:
if n%i==0:
print (i," ",end="")
divisor_recursive(n,i+1)
divisor_recursive(x)
0 Comments