Python Program to Calculate the Sum and the Average of N Numbers

Average of n numbers

Python programs to calculate the sum and the average of N numbers have been shown here. For example, if the numbers are 10, 20, 30, 40, 50 then the sum of these numbers would be 10 + 20 + 30 + 40 + 50 = 150 and average would be (150 / 5) = 30. The result can be obtained using list and also without using list.






1. Algorithm to find the sum and average of N numbers


1. Take number of terms $n$ as inputs.

2. Set $s = 0$ and $i = 0$

3. Take a number $x$ as input.

4. Perform $s = s + x$ and $i = i + 1$

5. Check if $i < n$, then go to step 3, else go to step 6

6. Display $s$ as the sum and $\frac{s}{n}$ as the average and exit program.




2. Pseudocode to find the sum and average of N numbers


Input: Number of terms $n$

Output: Sum and average of those $n$ terms

1. Procedure sumAverage($n$):

2. $sum \leftarrow 0$

3. $i \leftarrow 0$

4. Repeat for each $i < n$:

5. Read a number $x$

6. $sum \leftarrow sum + x$

7. $i \leftarrow i + 1$

8. $avg \leftarrow \frac{sum}{n}$

9. Return $sum$, $avg$

10. End Procedure





3. Time complexity to find the sum and average of N numbers


Time Complexity: O(n)

Here $n$ is the number of elements.





4. Python Program & output to calculate sum & average of n numbers without list

Code has been copied
# *************************************
#    	    alphabetacoder.com
# Python Program to calculate sum and
# average of n numbers without list
# *************************************

# take input of number of elements
n = int(input("Enter no of elements: "))

# initialize
s = 0

# take input of numbers one by one
# add each number to sum
print("Enter", n, "elements: ")
for i in range(1, n + 1):
    num = int(input(""))
    s = s + num

# calculate average
avg = float(s) / n

# display result
print("Sum:", s)
print("Average:", avg)

Output


Enter no of elements: 5

Enter 5 elements:

10

6

8

9

2

Sum: 35

Average: 7.0





5. Python Program to calculate sum & average of n numbers using list

Code has been copied
# *************************************
#    	    alphabetacoder.com
# Python Program to calculate sum and
# average of n numbers using array
# *************************************

# take input of number of elements
n = int(input("Enter no of elements: "))

# declare a list of size n
arr = [0] * n

# initialize
s = 0

# take input of numbers one by one
print("Enter", n, "elements: ")
for i in range(0, n):
    arr[i] = int(input(""))

# add each number to sum
for i in range(0, n):
    s = s + arr[i]

# calculate average
avg = float(s) / n

# display result
print("Sum:", s)
print("Average:", avg)

Output


Enter no of elements: 3

Enter 3 elements:

10

20

30

Sum: 60

Average: 20.0





6. Python Program to calculate sum & average of n numbers using sum() function

Code has been copied
# **********************************************
#    	        alphabetacoder.com
# Python Program to calculate sum and average
# of n numbers using sum() function
# **********************************************

# take input of number of elements
n = int(input("Enter no of elements: "))

# declare an array of size n
arr = [0] * n

# take input of numbers one by one
print("Enter", n, "elements: ")
for i in range(0, n):
    arr[i] = int(input(""))

# use sum() function to get the sum
s = sum(arr)

# calculate average
avg = float(s) / n

# display result
print("Sum:", s)
print("Average:", avg)

Output


Enter no of elements: 6

Enter 6 elements:

10

20

30

40

50

60

Sum: 210

Average: 35.0