8 Dec 2016

Recursion and File Handling demonstration using Python

The following program of Fibonacci series computes using recursive function and saves the output to a text file. The code is as shown below:

 # A program demonstrating recursion and file handling  
   
 x = 0 ; y = 1   
   
 def fibonacci(x,y,lim):  
      if lim != 0:  
           z = x + y  
           f.write('{0} '.format(z))  
           print('{0}'.format(z),end="\t")  
           x = y  
           y = z  
           lim -= 1  
           fibonacci(x,y,lim)  
        
 lim = int(input('Enter the size of series:'))            
 f = open('output.txt','w')  
 fibonacci(x,y,lim)  
 f.close()  
 print(end="\n")  

The output is shown in the following GIF image:


No comments:

Post a Comment