Python Program to find Sum and Average of n Numbers

14-Oct-2022

.

Admin

Python Program to find Sum and Average of n Numbers

Hi Dev,

Here, I will show you how to works Python Program to find Sum and Average of N Natural Numbers. I would like to share with you Python Exercise: Calculate the sum and average of n integer numbers. I’m going to show you about Calculate Sum and average of first n numbers in Python. This article will give you simple example of program to find SUM and AVERAGE of two numbers. You just need to some step to done How to calculate the sum and average of a given list in Python.

Sum and average of n natural numbers in python; Through this tutorial, you will learn how to how to find sum and average of n numbers in python program using for loop, while loop, function.

let's see below simple example with output:

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


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

n = int (n)

sum = 0

# loop from 0 to n

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 10

SUM of 10 numbers is:

55 Average of 10 natural number is: 5.5

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

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 10

sum of 10 using while loop 55

average of 10 using while loop 5.5

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

sum = 0

list = [10,24,46,20,15,5,30]

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 : 150

Average of list element is 21.428571428571

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

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 10

Sum of fthe irst 10 natural numbers using formula is: 55.0

Average of the first 10 natural numbers using formula is: 5.5

Example 5: Python Program to Find/Calculate average and sum of n odd natural 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

Example 6: Python Program to Find/Calculate average and sum of n even natural 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

I hope it can help you...

#Python