-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from Lucifer0420/patch-3
Program to find sum of array
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Python 3 code to find sum | ||
# of elements in given array | ||
def _sum(arr): | ||
|
||
# initialize a variable | ||
# to store the sum | ||
# while iterating through | ||
# the array later | ||
sum=0 | ||
|
||
# iterate through the array | ||
# and add each element to the sum variable | ||
# one at a time | ||
for i in arr: | ||
sum = sum + i | ||
|
||
return(sum) | ||
|
||
# driver function | ||
arr=[] | ||
# input values to list | ||
arr = [12, 3, 4, 15] | ||
|
||
# calculating length of array | ||
n = len(arr) | ||
|
||
ans = _sum(arr) | ||
|
||
# display sum | ||
print ('Sum of the array is ', ans) |