-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ5.py
More file actions
32 lines (29 loc) · 838 Bytes
/
Q5.py
File metadata and controls
32 lines (29 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'''5. Write a program in python to calculate the sum of elements in an array.
Test Data :
Input 5 elements in the array :
element - 0 : 5
element - 1 : 7
element - 2 : 3
element - 3 : 2
element - 4 : 9
Expected Output :
The sum of the elements of the array is 26'''
def create_array():
array = []
size = int(input('Enter the size of the array: '))
print(f'Input {size} elements in the array:')
for i in range(size):
element = int(input(f'Enter element - {i} : '))
array.append(element)
return array
def sum_of_array(arr):
total = 0
for num in arr:
total += num
return total
def main():
array = create_array()
result = sum_of_array(array)
print(f'The sum of the elements of the array is {result}')
if __name__ == "__main__":
main()