Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
Calculate the sum of elements in an array using Python
October 6, 2022
Sum of Elements in an array using Python
Here, in this page we will discuss the program to find the sum of elements in an array using Python programming language. We are given with an array and need to print the sum of its element. In this page we will discuss different ways to find the sum.
Methods Discussed in this page are :
Method 1 : Using Iteration
Method 2 : Using recursion
Method 3 : Using inbuilt Function
Method 1 :
Declare a variable say Sum =0 to hold the value of sum
n = int(input(“Enter the size of the array. \n”))
arr = []
for i in range(n):
element = int(input())
arr.append(element)
arr.sort()
print(f”The array are: {arr} \n”)
print(f”The sum of the total array are: {sum(arr)}”)
import numpy as np
n=int(input())
a=np.array([input().split() for i in range(n)] ,int)
print(sum(a))
import numpy as np
n=int(input())
s=0
a=np.array([input().split() for i in range(n)] ,int)
print(sum(a))
a1 = list(map(int,input().split(“,”)))
sum = 0
for i in a1 :
sum = sum + i;
print(sum);
n = int(input(“Enter the size of the array. \n”))
arr = []
for i in range(n):
element = int(input())
arr.append(element)
arr.sort()
print(f”The array are: {arr} \n”)
print(f”The sum of the total array are: {sum(arr)}”)
from array import *
a=array(“i”,[])
b=int(input(“enter size :”))
sum=0
for x in range(b):
c=int(input())
a.append(c)
sum=sum+c
print(sum)
n=int(input(“Enter how many number you want to add?”))
arr=[]
for i in range(n):
x=int(input(“Enter numbers”))
arr.append(x)
print(arr)
sum=0
for j in range(len(arr)):
sum=sum+arr[j]
print(sum)