How to Find the Average of 5 Numbers in Python

We will develop a program on how to find the average of 5 numbers in Python. The average is defined as the mean value which is equal to the ratio of the sum of the number of a given set of values to the total number of values present in the set. Here, we will calculate the average of five numbers using various methods.

How to find the average of numbers:

Average Formula = Total sum of all numbers / Number of item in the set
Average = (x1+x2+x3+…+xn)/n

Mathematically,

Inputs: a=2, b=5, c=8, d=2, e=3
Average = (a+b+c+d+e)/5 = (2+5+8+2+3)/5 = 20/5 = 5

Now let’s see how we can calculate average of five numbers in Python.

Python Program to Calculate the Average of 5 Number

This Python program is the simplest and easiest way to calculate the average of 5 numbers. We will take five numbers while declaring the variables and calculate the average value of those numbers using the average formula. Then, the average value will be displayed on the screen.

# Python program to find average of five numbers

# take inputs
num1 = 2
num2 = 3
num3 = 6
num4 = 8
num5 = 10

# calculate average
avg = (num1 + num2 + num3 + num4 + num5) / 5

# print average value
print('The average of numbers = %0.2f' %avg)

Output:-

The average of numbers = 5.80

Average of 5 Numbers in Python

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user. Inputs are scanned using the input() function and stored in variable num1, num2, num3, num4, and num5.

# Python program to find average of five numbers

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))
num4 = float(input('Enter four number: '))
num5 = float(input('Enter fifth number: '))

# calculate average
avg = (num1 + num2 + num3 + num4 + num5) / 5

# print average value
print('The average of numbers = %0.2f' %avg)

Output for the different input values:-

Enter first number: 2
Enter second number: 5
Enter third number: 3
Enter four number: 8
Enter fifth number: 12
The average of numbers = 6.00

Enter first number: 45
Enter second number: 89
Enter third number: 36
Enter four number: 20
Enter fifth number: 15
The average of numbers = 41.00

Find the Average of 5 Numbers using Functions

We can also take the help of a function to find the average of 5 numbers in python. A function is a block of code that performs a specific task.

# Python program to find average of five numbers using function

def avg_num(num1, num2, num3, num4, num5):   #user-defined function
    avg = (num1 + num2 + num3+ num4 + num5) / 5   #calculate average
    return avg    #return value

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))
num4 = float(input('Enter four number: '))
num5 = float(input('Enter fifth number: '))

# function call
average = avg_num(num1, num2, num3, num4, num5)

# display result
print('The average of numbers = ',average)

Output:-

Enter first number: 10
Enter second number: 20
Enter third number: 30
Enter four number: 20
Enter fifth number: 10
The average of numbers = 18.0

Take 5 integers from Keyboard using a Loop and Print their Average value on the Screen in Python

This python program also performs the same task but with different methods. In this program, we are using For Loop to take inputs and calculate the sum of those numbers. Then, calculate the average of those numbers and finally, print the average value.

# Python program to find the average of five numbers

# denotes total sum of five numbers
total_sum = 0

for n in range (5):
    # take inputs
    num = float(input('Enter number: '))
    # calculate total sum of numbers
    total_sum += num

# calculate average of numbers
avg = total_sum / 5

# print average value
print('Average of numbers =', avg)

Output:-

Enter number: 10
Enter number: 21
Enter number: 14
Enter number: 58
Enter number: 43
Average of numbers = 29.2

Also See:- How to find the average of 3 numbers in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply