Python Program to Find Average of n Numbers

Python program to find or calculate sum and average of n number; Through this tutorial, i am going to show you how to find sum and average of n number using while loop, for loop, function in python.

Python Program to Find Sum and Average of n Numbers

  • Write a Program Find/Calculate the Average of n natural numbers using loop in Python
  • Python Program Find Sum and Average of n natural numbers in python using while loop
  • Python Program to Calculate Sum and Average of N Numbers Using Math formula

Write a Program Calculate Sum & Average of n natural numbers using For loop in Python

n = input("Enter Number to calculate sum & average")
n = int (n)
sum = 0
for num in range(0, n+1, 1):
    sum = sum+num
    
average = sum / n
print("SUM of", n, "numbers is: ", sum )
print("Average of", n, "natural number is: ", average)

Output:

Enter Number to calculate sum & average 5 
SUM of 5 numbers is:  
15 Average of 5 natural number is:  3.0 

Python Program Find Sum and Average of n natural numbers in python using while loop

n = input("Enter Number to calculate sum and average")
n = int (n)
totalNo = n
sum=0
while (n >= 0):
    sum += n
    n-=1
    
average  = sum / totalNo
print ("sum of ", totalNo ,"using while loop ", sum)
print ("average of", totalNo ,"using while loop ", average)

Output:

Enter Number to calculate sum and average 5 
sum of  5 using while loop  15 
average of 5 using while loop  3.0 

Python Program to Calculate Sum and Average of N Numbers Using Math formula

n = input("Enter a number to calculate average and sum")
n = int (n)
sum = n * (n+1) / 2
average  = ( n * (n+1) / 2) / n
print("Sum of the first ", n, "natural numbers using formula is: ", sum )
print("Average of the first ", n, "natural numbers using formula is: ", average )

Output:

Enter a number to calculate average and sum 5 
Sum of the first  5 natural numbers using formula is:  15.0 
Average of the first  5 natural numbers using formula is:  3.0   

Recommended Python Tutorials

Recommended:-Python Map() Method

Be First to Comment

Leave a Reply

Your email address will not be published.