Python Program to Find Sum of 1+11+111 Using Recursion

In this Python program, we read number of terms term from user. Then we pass value of term to recSum() function which calculates sum of 1 + 11 + ... series up to n terms recursively.


This Python program calculates sum of 1 + 11 + 111 + 1111 + ... up to n terms using recursive function.


Python Source Code


# Sum of 1+11+111+... using recursion

def recSum(n):
    if n<=0:
        return 0
    else:
        return (n + 10 * recSum(n-1))

# Reading number of terms
term = int(input("Enter number of terms:"))
series_sum = recSum(term)
print("\nSum of series is: ", series_sum)

Output

Enter number of terms:5

Sum of series is:  12345