Q . A Geometric Progression (GP) is a progression where the each term is a multiple of the previous one. The multiplying factor is called the common ratio.

So a GP with a first term a and a common ratio r with n terms, can be stated as:

a, ar, ar2, ar³, ar4….  arn-1

 

(a) Write a recursive function that prints a GP. Input a, r and n in_main_part..

 

(b) Write a recursive function that calculates the sum of a GP by changing the function that you wrote in part (a). Obtain a, r and n in_main_ part. Highlight the changes that were made to get the desired result.

 

Answer =


#(a)
a = int(input("Enter first term :-"))
r = int(input("Enter common ratio :-"))
term = int(input("Enter term :-"))
def gp(a,r,n):
    if n == term :
        return 0
    else :
        print(a*r**n,end=",")
        return gp(a,r,n+1)
gp(a,r,0)


#(b)
a = int(input("Enter first term :-"))
r = int(input("Enter common ratio :-"))
term = int(input("Enter term :-"))
def gp(a,r,n):
    if n == term :
        return 0
    else :
        print(a*r**n,end=",")
        return a*r**n + gp(a,r,n+1)
    
print("Sum",gp(a,r,0))


Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post