Python Program To Find Average Of n Numbers Using For Loop

Python Program To Find Average Of n Numbers Using For Loop

In this tutorial, you will learn to write a Python Program To Find Average Of n Numbers Using For 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. For Loop

Python Program To Find Average Of n Numbers Using For Loop

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

for i in range(1, n+1):
    num = float(input("Enter element " + str(i) + ": "))
    sum += num

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

Output

Enter the number of elements to be inserted: 5
Enter element 1: 5
Enter element 2: 10
Enter element 3: 15
Enter element 4: 20
Enter element 5: 25
Average of 5 numbers is: 15.0

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

We then use a for 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.


Discover more from Python Mania

Subscribe to get the latest posts sent to your email.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Related Articles:

Recent Articles:

0
Would love your thoughts, please comment.x
()
x

Discover more from Python Mania

Subscribe now to keep reading and get access to the full archive.

Continue reading