How to find the sum and average of first n natural numbers in Python? In this article, we are going to write a Python code to calculate the sum and average of N natural number using for loop.
The algorithm to calculate the sum and average of first n natural numbers
- This Python program allows the user to enter the number (n) he wishes to calculate the average. The program accepts user input using the input function.
- Next, the user has to enter the individual number up to a declared number.
- Iterate till the entered number using the for loop and range() function.
- Next, calculate the sum using a
sum = sum + current numberformula.
- Later calculate the average using
sum / n.
nis a number entered by the user.
Python Program to calculate the Sum
n = input("Enter Number to calculate sum") n = int (n) average = 0 sum = 0 for num in range(0,n+1,1): sum = sum+num print("SUM of first ", n, "numbers is: ", sum )
Enter Number to calculate sum 5 Sum of first 5 number is: 15
Python Program to calculate average
print ("calculate an average of first n natural numbers") n = input("Enter Number ") n = int (n) average = 0 sum = 0 for num in range(0,n+1,1): sum = sum+num; average = sum / n print("Average of first ", n, "number is: ", average)
calculate an average of first n natural numbers Enter Number 10 Average of first 10 number is: 5.5
Calculate the sum and average of a given list in Python
Let see you have a list of numbers and you want to calculate the sum of numbers present in the list.
- All you need to do is to iterate a list using a for loop and add each number to a sum variable.
- Calculate the length of a given list(total numbers in a list) and divide the sum by total length to calculate average.
Let see this with an example.
sum = 0 list = [11,22,33,44,55,66,77] for num in list: sum = sum +num average = sum / len(list) print ("sum of list element is : ", sum) print ("Average of list element is ", average )
sum of list element is : 308 Average of list element is 44.0
Calculate sum using built-in sum function in Python
Let’s see how to use a
sum() function to calculate the sum.
print ("Sum of range of numbers: ", sum(range(1,5)))
Sum of range of numbers: 10
Python while loop to calculate sum and average
You can also use while loop to calculate the sum and average of n numbers. Here, n can be 2, 5 or any number.
Follow these steps:
- Take a value of n =20
- Run while loop until n is greater than zero
- Add the current value of n to sum variable. Also, decrement n by 1 in while loop body
- calculate the average by dividing the sum by total numbers.
Let see the demo to calculate the sum and average using while loop.
n = 20 total_numbers = n sum=0 while (n >= 0): sum += n n-=1 print ("sum using while loop ", sum) average = sum / total_numbers print("Average using a while loop ", average)
sum using while loop 210 Average using a while loop 10.5
To practice what you learned in this article, I have created a Python Basic Quiz and Exercise.
Let me know your comments and feedback in the section below.