Python Program To Find Average Of n Numbers Using While Loop

Python Program To Find Average Of n Numbers Using While Loop

In this tutorial, you will learn to write a Python Program To Find Average Of n Numbers Using While Loop.

The average of n numbers is the sum of those n numbers divided by n.

For example, the average of 1, 2, 3, 4, and 5 is (1+2+3+4+5)/5 = 3.

To understand this program you should have an understanding of the following topics:

  1. While Loop

1: Python Program To Find Average Of n Numbers Using While Loop

n = int(input("Enter the number of elements to be inserted: "))
sum = 0
i = 1

while i <= n:
    num = float(input("Enter element " + str(i) + ": "))
    sum += num
    i += 1

avg = sum / n
print("Average of", n, "numbers is: ", avg)

Output

Enter the number of elements to be inserted: 4
Enter element 1: 4
Enter element 2: 8
Enter element 3: 12
Enter element 4: 16
Average of 4 numbers is: 10.0

In this program, we first take input from the user to determine the number of elements to be inserted.

We then use a while loop to take input for each element and add it to a running sum.

After all elements have been added, we calculate the average by dividing the sum by the number of elements and print the result.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x