Python Program to find Average of N Numbers
You are Here:
Using for loop
In the following example, we will find the average of numbers between 1 and 10 using for loop.
Example
Python Compiler
import math
start = 1;
end = 10;
total = 0;
count = end - start + 1
print("Average of numbers between %d and %d:\n" % (start, end))
for start in range(start, end+1):
total += start
print("Total = %d" % total)
print("Count = %d" % count)
print("Average is %.2f" % float(total/count))
Output
Average of numbers between 1 and 10:
Total = 55
Count = 10
Average is 5.50
Using while loop
In the following example, we will find the average of numbers between 1 and 10 using while loop.
Example
Python Compiler
import math
start = 1;
end = 10;
total = 0;
count = end - start + 1
print("Average of numbers between %d and %d:\n" % (start, end))
while(start <= end):
total += start;
start += 1;
print("Total = %d" % total)
print("Count = %d" % count)
print("Average is %.2f" % float(total/count))
Output
Average of numbers between 1 and 10:
Total = 55
Count = 10
Average is 5.50
Find Average of N Numbers for any Given Range
In the following example, we will find the average of N numbers between the user given range.
Example
Python Compiler
import math
start = int(input("Enter a (int) starting number: "));
end = int(input("Enter a (int) ending number: "));
total = 0;
count = end - start + 1
print("Average of numbers between %d and %d:\n" % (start, end))
while(start <= end):
total += start;
start += 1;
print("Total = %d" % total)
print("Count = %d" % count)
print("Average is %.2f" % float(total/count))
Output
Enter a (int) starting number: 5
Enter a (int) ending number: 10
Average of numbers between 5 and 10:
Total = 45
Count = 6
Average is 7.50
Reminder
Hi Developers, we almost covered 90% of String functions and Interview Question on Python with examples for quick and easy learning.
We are working to cover every Single Concept in Python.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.