Skip to content
Exercises · Q8

Q.Write a program to find the average of 3 numbers.

Puducherry CbseNCERTSubjective· 3mImportance★★★★★est
84% · 21/25 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

This program takes three numbers as input from the user, calculates their sum, and then divides the sum by 3 to find their average, which is then displayed.

To find the average of a set of numbers, we apply a fundamental mathematical concept: the average (or mean) is calculated by summing all the numbers in the set and then dividing that sum by the count of numbers in the set. In this specific problem, we are dealing with exactly three numbers.

Algorithm to find the average of 3 numbers

  1. Input: Get the three numbers from the user. It's good practice to allow for decimal numbers, so we'll treat them as floating-point numbers.
  2. Summation: Add these three numbers together to find their total sum.
  3. Division: Divide the calculated sum by 3 (since there are three numbers).
  4. Output: Display the result as the average.

Python Program

# Step 1: Get input for three numbers from the user
print("Enter three numbers to calculate their average.")
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

# Step 2: Calculate the sum of the three numbers
total_sum = num1 + num2 + num3

# Step 3: Calculate the average by dividing the sum by 3
average = total_sum / 3

# Step 4: Display the calculated average
print(f"The three numbers entered are: {num1}, {num2}, {num3}")
print(f"Their sum is: {total_sum}")
print(f"The average of the three numbers is: {average}")

Explanation of Key Lines

  1. num1 = float(input("Enter the first number: "))

    • input("Enter the first number: "): This function is used to get input from the user. The string inside the parentheses is displayed as a prompt to the user. The user's input is always read as a string.
    • float(...): Since we want to perform mathematical operations (addition and division) on the numbers, we need to convert the input string into a numerical type. float() converts the string to a floating-point number, which can represent both whole numbers and decimals. Using float is generally safer than int for averages, as averages often result in decimal values.
    • num1 = ...: The converted numerical value is then stored in the variable num1. The same logic applies to num2 and num3.
  2. total_sum = num1 + num2 + num3

    • This line performs the core arithmetic operation of summation. It adds the values stored in num1, num2, and num3 and assigns the result to the variable total_sum.
  3. average = total_sum / 3

    • This line calculates the average. It divides the total_sum by 3 (the count of numbers) using the division operator /. The result is stored in the average variable. Python's / operator always performs float division, which is appropriate here.
  4. print(f"The average of the three numbers is: {average}")

    • print(...): This function is used to display output to the console. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.