Question:1
Write a python script to take input for a limit, calculate and print sum of all the numbers upto to the limit? (using recursion)
Solution:
#rec_1.py #sum of all the numbers upto to a limit def rec_sum(n): if(n==0): return(0) else: return(n+rec_sum(n-1)) #function calling n=int(input("Enter limit ")) s=rec_sum(n) print("Sum = ",s)
Question:2
Write a python script to take input for a limit, calculate and print product of all the numbers upto to the limit? (using recursion)
#rec_2.py #prod of all the numbers upto to a limit def rec_prod(n): if(n==0): return(1) else: return(n*rec_prod(n-1)) #function calling n=int(input("Enter limit ")) s=rec_prod(n) print("product = ",s)
Question:3
Write a python script to take input for a number , calculate and print its factorial? (using recursion)
#rec_3.py #factorial of a number def rec_fact(n): if(n==0): return(1) else: return(n*rec_fact(n-1)) #function calling n=int(input("Enter any number ")) f=rec_fact(n) print("Factorial = ",f)