0% found this document useful (0 votes)
6 views2 pages

Python Lab 2a, 2b

The document contains Python code for generating a Fibonacci sequence of a specified length and calculating the binomial coefficient using factorials. The Fibonacci sequence is printed based on user input, while the binomial coefficient is computed based on values of N and R provided by the user. Sample outputs demonstrate the functionality of both programs.

Uploaded by

shravaniav2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Python Lab 2a, 2b

The document contains Python code for generating a Fibonacci sequence of a specified length and calculating the binomial coefficient using factorials. The Fibonacci sequence is printed based on user input, while the binomial coefficient is computed based on values of N and R provided by the user. Sample outputs demonstrate the functionality of both programs.

Uploaded by

shravaniav2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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

You might also like