Example HTML page

A Python program to perform arithmetic operations on array

In this post i will show you how to perform arithmetic operations on array by using if-else and for loops inside a function.

#Program on addition of elements in a given array.

def add(arr):
    return(sum(arr))
arr=[ ]
arr = [5,8,6,13,26,16] 
res = add(arr) 
print ("Sum of the array elements is:",res)

Output

Sum of the array elements is: 74

 We can also call the function definition directly without storing the resultant in to any variable and is shown in below:

def add(arr):
    return(sum(arr))
arr=[ ]
arr = [5,8,6,13,26,16] 
print(add(arr))

Output

74

 Explanation

To add the elements in a given array simply we are defining a function ‘add’ and passed ‘arr’ as a parameter in to it. The ‘return’ statement returns the sum of array elements by using in-built function ‘sum( )’. An empty array is created and is stored in ‘arr’ variable. Append some elements in to it. Now, we call the function definition ‘add(arr)’ and stored in ‘res’ variable and finally we print the result.

Using if-else

#The above same program can also be written by using if-else inside a function.

def add(arr): 
     if len(arr)== 1: 
        return arr[0] 
     else: 
        return arr[0]+add(arr[1:])
arr =[ ] 
arr = [26,52,8,5,6] 
res =add(arr) 
print (res)

Output

Sum of array elements:  97

Similarly we can also write the programs for subtraction, multiplication and division of array elements using if-else inside a function.

#Program on Subtraction of array elements using if-else inside a function.

def sub(arr): 
     if len(arr)== 1: 
        return arr[0] 
     else: 
        return arr[0]-sub(arr[1:])
arr =[ ] 
arr = [26,52,8,5,6] 
res =sub(arr) 
print ("Subtraction of array elements: ",res)

Output

Subtraction of array elements:  -17

#Program on Multiplication of array elements using if-else inside a function.

def mul(arr): 
     if len(arr)== 1: 
        return arr[0] 
     else: 
        return arr[0]*mul(arr[1:])
arr =[ ] 
arr = [26,52,8,5,6] 
res =mul(arr) 
print ("Multiplication of array elements: ",res)

Output

Multiplication of array elements:  324480

 #Program on Division of array elements using if-else inside a function.

def div(arr): 
     if len(arr)== 1: 
        return arr[0] 
     else: 
        return arr[0]/div(arr[1:])
arr =[] 
arr = [26,52,8,5,6] 
res =div(arr) 
print ("Division of array elements: ",res)

Output

Division of array elements:  4.8

Using for loops:

The following are the programs of addition, subtraction, multiplication and division of array elements by using for loops inside the function:

#Program on addition of array elements using for loops inside a function:

def add( arr , n ): 
    sum = 1
    for i in range(n): 
        sum = sum + arr[i] 
    return sum
arr = [1, 2, 3, 4, 5, 6] 
n = len(arr) 
print("Addition of array elements: ",add(arr, n))

Note:

We initialize result as 1. We traverse array from left to right and multiply elements with result.

Output

Addition of array elements:  22

#Program on subtraction of array elements using for loops inside a function:

def sub( arr , n ): 
    minus = 1
    for i in range(n): 
        minus = minus - arr[i] 
    return minus
arr = [6,4,3,2,1] 
n = len(arr) 
print("Subtraction of array elements: ",sub(arr, n))

Output

Subtraction of array elements:  -15

#Program on multiplication of array elements using for loops inside a function

def mul( arr , n ): 
    product = 1
    for i in range(n): 
        product = product * arr[i] 
    return product
arr = [6,4,3,2,1] 
n = len(arr) 
print("Multiplication of array elements: ",mul(arr, n))

Output

Multiplication of array elements:  144

#Program on Division of array elements using for loops inside a function

def div( arr , n ): 
    d = 1
    for i in range(n): 
        d = d / arr[i] 
    return d
arr = [2,1] 
n = len(arr) 
print("Division of array elements: ",div(arr, n))

Output

Division of array elements:  0.5

 

 

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest