0% found this document useful (0 votes)
35 views

2nd Year 2nd Assignment in Python Programming

The document describes a Python program that uses recursion to calculate the Fibonacci sequence. It takes a number from the user as input and uses a recursive function to calculate the Fibonacci numbers up to the input number. The recursive function returns the nth Fibonacci number by adding the results of calling itself on the previous two numbers. It initializes the first two numbers to 1 and stores the calculated sequence in an array. It then prints the output Fibonacci sequence.

Uploaded by

Lovekush Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

2nd Year 2nd Assignment in Python Programming

The document describes a Python program that uses recursion to calculate the Fibonacci sequence. It takes a number from the user as input and uses a recursive function to calculate the Fibonacci numbers up to the input number. The recursive function returns the nth Fibonacci number by adding the results of calling itself on the previous two numbers. It initializes the first two numbers to 1 and stores the calculated sequence in an array. It then prints the output Fibonacci sequence.

Uploaded by

Lovekush Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Programming class assignment -2

Name :-lovekush kumar # roll no:-27600120018 #sec :-A

Q:- write a programme in python to input number and find Fibonacci series using recursion.

#All programme are written and compiled in python

#Python programme to input number and find Fibonacci series using recursion.

# take a numbers from user find Fibonacci series using recursion.

#Algorithm
1. Initialize an array arr of size n to zeros.
2. If n equals 0 or 1; return 1 Else.
3. Initalize arr[0] and arr[1] to 1.
4. Run for loop in range[2,num]
5. Compute the value arr[I]=arr[I-1] +arr[I-2]
6. The array has the sequence computed till n.

#flowchart

Program:-
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
Programming class assignment -2
Name :-lovekush kumar # roll no:-27600120018 #sec :-A
# take input from the user
nterms = int(input("How many terms? "))
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))

input:-
How many terms? 10

output:
== RESTART: D:\loveraj\love\love programming\python programming\-----recur.py ==

Fibonacci sequence:

13

21

34

>>>
Programming class assignment -2
Name :-lovekush kumar # roll no:-27600120018 #sec :-A

You might also like