Learn about Python Program to Find Average of N Numbers using While Loop in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Python Program to Find Average of N Numbers using While Loop
Initially, input is taken from the user and then the total sum of input numbers is calculated using a while loop. The total sum is then divided by the total numbers given.
Source code:
# Python program to find the average of n numbers
# total number count
n = float(input('How many numbers: '))
total_sum = 0
i =1
while i <= n:
num = float(input('Enter number: '))
total_sum += num
i = i+1
# calculate average of numbers
avg = total_sum / n
# print value
print('The average of numbers = %0.2f' %avg)
Output:
How many numbers: 3 Enter number: 2 Enter number: 7 Enter number: 10 The average of numbers = 6.33
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Program to Find Average of N Numbers in Python