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.
3.
4.
5.
6.
7.
8.
9.
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
# ************************************* # 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
# ************************************* # 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
# ********************************************** # 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