2a. Develop a program to generate Fibonacci sequence of length (N).
Read N
from the console.
PYTHON CODE
num = int(input("Enter the Fibonacci sequence length to be generated : "))
firstTerm = 0
secondTerm = 1
print("The Fibonacci series with", num, "terms is :")
print(firstTerm, secondTerm, end=" ")
for i in range(2,num):
curTerm = firstTerm + secondTerm
print(curTerm, end=" ")
firstTerm = secondTerm
secondTerm = curTerm
print()
OUTPUT 1
Enter the Fibonacci sequence length to be generated : 5
The Fibonacci series with 5 terms is :
01123
OUTPUT 2
Enter the Fibonacci sequence length to be generated : 8
The Fibonacci series with 8 terms is :
0 1 1 2 3 5 8 13
2b. Write a function to calculate factorial of a number. Develop a program to compute
binomial co- efficient (Given N and R).
PYTHON CODE
def fact(num):
if num == 0:
return 1
else:
return num * fact(num-1)
n = int(input("Enter the value of N : "))
r = int(input("Enter the value of R (R cannot be negative or greater than N): "))
nCr = fact(n)/(fact(r)*fact(n-r))
print(n,'C',r," = ","%d"%nCr,sep="")
OUTPUT 1
Enter the value of N : 7
Enter the value of R (R cannot be negative or greater than N): 5
7C5 = 21
OUTPUT 2
Enter the value of N : 8
Enter the value of R (R cannot be negative or greater than N): 5
8C5 = 56