• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Python

Python Programs on Recursion

  • Categories Python, Python Recursion
Python Recursion

Python Programs on Recursion:

1.Write a program using a recursive function to print Fibonacci series up to nth term.       
Ans.
def fibo(n):
    if n==1:
        return 0
    elif n == 2:
        return 1
    else:
        return fibo(n-1)+fibo(n-2)
 
n=int(input("enter no of fibonnaci nos to be printed:"))
for i in range(1,n+1):
    print(fibo(i),end=',')   
 
2.Write a recursive function to implement binary search algorithm.
Ans.
def binsearch(array,key,low,high):
    if low>high:
        return -1
    mid=int((low+high)/2)
    if key==array[mid]:
        return mid
    elif key <array[mid]:
        high = mid-1
        return binsearch(array,key,low,high)
    else:
        low=mid+1
        return binsearch(array,key,low,high)
 
sortedarray=[10,5,12,15,20,31,34]
item= int(input("enter search item:"))
res=binsearch(sortedarray,item,0,len(sortedarray)-1)
if res>=0:
    print("Found")
else :
    print ("Not Found")
 
3.Write recursive function to take in a string to print it backwards.
Ans.
def backprint(S):
    n = len(S)
    if n == 0:
        return
    print(S[-1], end = '')
    backprint (S[0:-1])
# _main_
s = input("Enter a string : ")
backprint(s)
 
4.Write a program to show the use of recursion in calculation of power of a number.
Ans.
def power(a, b) :
     if b == 0 :
             return 1
     else :
             return a * power(a, b-1)
 
n = int (input("Enter number :"))
p = int(input ("Enter power of : "))
result = power(n,p)
print(n, "raised to the power of", p, "is", result)
 
5.Write recursive code to compute greatest common divisor of two numbers.
Ans.
def gcd(a,b):
    if(b==0):
        return a
    else:
        return gcd(b,a%b)
 
n1=int(input("enter first number:"))
n2=int(input("enter second number:"))
d=gcd(n1,n2)
print("GCD of", n1, "and",n2,"is:",d) 
 
6.Write recursive code to compute and print sum of squares of numbers. Value of n is passed as parameter.                 
Ans.
def sqsum(n):
    if n==1:
        return 1
    return n*n+sqsum(n-1)
 
n=int(input("enter value of n:"))
print(sqsum(n))n

  • Share:
author avatar
Simply Coding

Previous post

Python Programs on Tuples
April 22, 2020

Next post

Python Programs on Lists
April 22, 2020

You may also like

character python
Solve any character pattern program in python
3 June, 2021
number pattern in python
Solve any number patterns programs in python
3 June, 2021
Star Pattern program in Python
Star Pattern Programs In Python
20 May, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now