Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In python, how can i get the multiplication of array elements using recursion?

I want to get the multiplication of array elements using recursion.

arr is array, and n is length of the array.

if arr=[1, 2, 3], n=3, answer is 6.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried this, but error occurred.

def multiply(arr, n):
    if n == 0:
        return arr
    else:
        return arr[n] * \
               multyply(arr[n - 1])

please help me.

>Solution :

You should implement it like this

def mul(arr):
    if not arr:
        return 1
    return arr[0] * mul(arr[1:])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading