Skip to main content

← Back to Overview

Sum and Average of Three Numbers

Concept Explanation

What is it?

This concept involves taking three numerical inputs, computing their sum, and then determining their average. The sum is the total value obtained by adding all numbers together, while the average (or mean) is the sum divided by the count of the numbers.

Why is it important?

Calculating sums and averages is a fundamental operation in many fields, including statistics, data analysis, and everyday calculations. It helps summarize data, understand central tendencies, and forms the basis for more complex statistical analyses.

Where is it used?

  • Financial Applications: Calculating average monthly expenses, profits, or stock prices.
  • Scientific Research: Finding average readings from experiments.
  • Education: Calculating average grades.
  • Data Analysis: Summarizing datasets.

Real-world example

If you want to know your average score across three tests, you add up the scores and divide by three. Similarly, if a company wants to find the average sales per month over a quarter, they sum up the sales for three months and divide by three.


Algorithm

  1. Start.
  2. Get three numbers (num1, num2, num3) from the user.
  3. Calculate the sum: sum = num1 + num2 + num3.
  4. Calculate the average: average = sum / 3.
  5. Display the calculated sum and average.
  6. End.

Edge Cases:

  • Non-numeric input (handled by language-specific error mechanisms or explicit validation).
  • Division by zero is not an issue here as we divide by a fixed number (3).

Implementations

import java.util.Scanner;

public class SumAndAverageOfThreeNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();

System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();

System.out.print("Enter the third number: ");
double num3 = scanner.nextDouble();

// Calculate sum
double sum = num1 + num2 + num3;

// Calculate average
double average = sum / 3;

System.out.println("Numbers: " + num1 + ", " + num2 + ", " + num3);
System.out.println("Sum: " + sum);
System.out.println("Average: " + String.format("%.2f", average));

scanner.close();
}
}

Explanation

  • Java: Uses Scanner to get three double inputs. Performs addition and division. String.format("%.2f", average) is used for formatted output of the average.
  • Python: Uses input() to get three string inputs, which are then converted to float. f-strings are used for formatted output.
  • C: Employs scanf("%lf", ...) to read three double inputs and printf("%.2lf, ...) for formatted output.
  • Oracle: Implemented in PL/SQL. Uses substitution variables (&Enter_First_Number, etc.) for user input (which get prompted at runtime in SQL*Plus/SQL Developer). Calculates sum and average, and displays with DBMS_OUTPUT.PUT_LINE and ROUND for formatting.

Complexity Analysis

  • Time Complexity: O(1) - The number of operations is constant, regardless of the input values.
  • Space Complexity: O(1) - A fixed number of variables are used.

Flowchart

Sample Dry Run

Stepnum1num2num3SumAverageDescription
Input102030--User enters three numbers
Process10203060-Sum = 10 + 20 + 30
Process1020306020Average = 60 / 3
Output---6020Display Sum (60) and Average (20)

Practice Problems

Easy

  • Modify the program to calculate the sum and average of four numbers.
  • Take numbers as input until the user enters 0, then calculate their sum and average.

Medium

  • Implement a function/method to calculate the average of an array/list of numbers.
  • Add input validation to ensure only numbers are entered.

Hard

  • Calculate the weighted average of numbers, where each number has a specific weight.
  • Calculate standard deviation along with the average.

"The difference between a successful person and others is not a lack of strength, not a lack of knowledge, but rather a lack of will." - Vince Lombardi

📍 Visit Us

🏫 VD Computer Tuition Surat

VD Computer Tuition
📍 Address
2/66 Faram Street, Rustompura
Surat395002, Gujarat, India
📞 Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition — Areas We Serve in Surat

AdajanAlthanAmroliAthwaAthwalinesBhagalBhatarBhestanCanal RoadChowkCitylightDumasGaurav PathGhod Dod RoadHaziraJahangirpuraKamrejKapodraKatargamLimbayatMagdallaMajura GateMota VarachhaNanpuraNew CitylightOlpadPalPandesaraParle PointPiplodPunaRanderRing RoadRustampuraSachinSalabatpuraSarthanaSosyo CircleUdhnaVarachhaVed RoadVesuVIP Road
📞 Call Sir💬 WhatsApp Sir