Python Program to Find/Calculate Average of 3, 4, 5…n numbers

Python Program to Find/Calculate Average of 3, 4, 5…n numbers

The Objective of this post, you will see various Python examples that cover the following:

  • Calculate/Find the Average of n natural numbers using loop and range function.
  • Find/Calculate Average of n natural numbers in python using while loop
  • Python program to find/calculate the Average of numbers in a given list
  • The mathematical formula to find/calculate the Average of n numbers with python program
  • Python Program to Find/Calculate Average of n odd natural numbers.
  • Python Program to Find/Calculate Average of n even natural numbers.

Now let’s see each one by one:

1: Find/Calculate the Average of n natural numbers using loop and range function

  • Use a python input() function in your python program that takes an input from the user to enter the number (n) to calculate the sum.
  • Next, declare variables that name sum, avg. avg will contain an average of n natural numbers average.
  • Next, run loop till the entered number using the for loop and range() function.
  • Inside a loop, calculate the sum of n numbers using a sum = sum+num formula.
  • Next, calculate the average of n numbers using a average = sum / n formula.
  • After the loop ends, print the sum, avg variable that contains the sum , average of n numbers

Python program to find the sum and average of n numbers using for loop:

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 

2: Find/Calculate Sum of n natural numbers in python using while loop

Also, use the while loop in python to calculate the sum of n numbers.

  • First of all, you can use a python input() function in your python program that takes input from a user to enter the number (n) to calculate the sum and average.
  • Next, declare variables that name sum and avg. avg will contain the avg of n natural numbers sum.
  • Run while loop until n is greater than zero
  • Add the current value of n to sum variable. And, decrement n number by 1 in while loop body.
  • Next, calculate the average of n numbers using a average = sum / n formula.
  • After the loop finishes, the print sum and avg name variable.

Python program to find the sum and average of n numbers 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 

3: Python program to Find/Calculate the average and sum of numbers in a given list

  • Declare a variable that name avg and sum. Avg will contain the average of n natural numbers.
  • Next, define list and assign a value to a python list.
  • Run for a loop and Add the current value of n to sum variable.
  • Next, calculate the average of n numbers.
  • After the loop finishes, the print sum name variable.
sum = 0

list = [11,4,5,7,99,10,12]

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 )

Output:

sum of list element is :  148 
Average of list element is  21.142857142857142 

4: The mathematical formula to Find/Calculate the sum of n numbers with python program

In the above programs, you have learned how to calculate the sum of n numbers using for loop, while loop and range function.

Now, you will learn how to calculate/find sum of n numbers in python without for loop, while loop in python. Calculate the sum directly using a mathematical formula in python program.

The sum of the n natural number mathematical formula is = n * (n+1) / 2.

In the below python program, you will learn how to use this mathematical formula is = n * (n+1) / 2 to find/calculate sum of n numbers in python programs.

Follow the steps:

  • Take input from user in your python program using input() function.
  • Convert a user inputted number to an integer using int() function.
  • Calculates sum of number by using this formula n * (n+1) / 2 in your python program.
  • Next, Calcuate average of numbers by using this formula average = ( n * (n+1) / 2) / n in your python program.
  • After that, the print name sum variable.
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 fthe irst ", 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 fthe irst  5 natural numbers using formula is:  15.0 
Average of the first  5 natural numbers using formula is:  3.0   

5: Python Program to Find/Calculate average and sum of n odd natural numbers

  • Take input from user using python input() function in your python program.
  • Next, declare a variable that name sum, it will contain the sum of n odd numbers.
  • Next, run loop till the entered number using the for loop and range() function.
  • Inside a loop, calculate the sum of n odd numbers using a sum = sum + current number formula with (not (num % 2) == 0).
  • Next, calculate the average of n odd numbers.
  • After the loop ends, print the sum variable that contains the sum of n odd numbers.

Python program to find average and sum of n odd numbers:

n = input("Enter Number to calculate average and sum")

n = int (n)

sum = 0

for num in range(0, n+1, 1):
    
    if(not (num % 2) == 0):
      sum += num;

average  = sum / n

print("SUM of odd numbers is: ", sum )
print("Average of odd numbers is: ", average )

Output:

Enter Number to calculate sum 5 
SUM of odd numbers is:  9 

6: Python Program to Find/Calculate average and sum of n even natural numbers

  • Take input from the user using python input() function in your python program.
  • Next, declare a variable that name sum, it will contain the sum of n even numbers.
  • Next, run loop till the entered number using the for loop and range() function.
  • Inside a loop, calculate the sum of n even numbers using a sum = sum + current number formula with if test condition ((num % 2) == 0).
  • Next, calculate the average of n even numbers.
  • After the loop ends, print the sum variable that contains the sum of n even numbers.

Python program to find average and sum of n even numbers:

n = input("Enter Number to calculate average and sum")

n = int (n)

sum = 0

for num in range(0, n+1, 1):
    
    if((num % 2) == 0):
      sum += num;
      

average  = sum / n

print("SUM of even numbers is: ", sum )
print("Average of even numbers is: ", average )

Output:

Enter Number to calculate average and sum 5 
SUM of even numbers is:  6 
Average of even numbers is:  1.2 

Leave a Reply

Your email address will not be published. Required fields are marked *