109k views
1 vote
Write a recursive function that calculates the sum 11 22 33 ... nn, given an integer value of nin between 1 and 9. You can write a separate power function in this process and call that power function as needed:

User Andre Lee
by
5.8k points

1 Answer

4 votes

Answer:

The function in Python is as follows:

def sumDig(n):

if n == 1:

return 11

else:

return n*11 + sumDig(n - 1)

Step-by-step explanation:

This defines the function

def sumDig(n):

This represents the base case (where n = 1)

if n == 1:

The function returns 11, when it gets to the base case

return 11

For every other value of n (n > 1)

else:

This calculates the required sum recursively

return n*11 + sumDig(n - 1)

User Axiom
by
6.1k points