Average of 3 Numbers

Average of 3 Numbers in C++ (4 Programs)

C++Beginner
C++
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    float num1, num2, num3;
    
    cout << "Enter three numbers: ";
    cin >> num1 >> num2 >> num3;
    
    float average = (num1 + num2 + num3) / 3.0;
    
    cout << fixed << setprecision(2);
    cout << "Average of " << num1 << ", " << num2 << ", and " << num3 
         << " = " << average << endl;
    
    return 0;
}

Output

Enter three numbers: 10 20 30
Average of 10.00, 20.00, and 30.00 = 20.00

This program teaches you how to calculate the average (arithmetic mean) of three numbers in C++. The average is one of the most fundamental statistical measures, calculated by summing all numbers and dividing by the count. This program demonstrates the basic formula and various implementation approaches.


1. What This Program Does

The program calculates the average of three numbers. For example:

  • Input: 10, 20, 30
  • Calculation: (10 + 20 + 30) / 3 = 60 / 3 = 20
  • Output: 20.00

The average represents the central value of a set of numbers, providing a single value that summarizes the data.

Example:

  • Numbers: 5, 10, 15 → Average: (5 + 10 + 15) / 3 = 10
  • Numbers: 7.5, 8.5, 9.5 → Average: (7.5 + 8.5 + 9.5) / 3 = 8.5

2. Header Files Used

  1. #include <iostream>

    • Provides cout and cin for input/output operations.
  2. #include <iomanip>

    • Provides setprecision() and fixed for formatting decimal output.
    • Essential for displaying averages with specific decimal places.

3. Declaring Variables

The program declares: float num1, num2, num3;

  • float is used to handle decimal numbers.
  • num1, num2, num3 store the three numbers entered by the user.

4. Taking Input From the User

The program asks: cout << "Enter three numbers: "; cin >> num1 >> num2 >> num3;

The user enters three numbers, for example: 10 20 30

The >> operator can chain multiple inputs, reading them sequentially.


5. Calculating the Average

The calculation is done using: float average = (num1 + num2 + num3) / 3.0;

How it works:

  • First, sum all three numbers: num1 + num2 + num3
  • Then, divide by 3.0 (using 3.0 ensures floating-point division)
  • Store the result in the variable average

Why 3.0 instead of 3?:

  • Using 3.0 ensures floating-point division.
  • If you use 3 (integer), the division might truncate to integer.
  • Example: (10 + 20 + 30) / 3.0 = 20.0 (correct)
  • Example: (10 + 20 + 30) / 3 = 20 (works here, but can lose precision)

6. Formatting the Output

To display the result with 2 decimal places: cout << fixed << setprecision(2);

  • fixed displays numbers in fixed decimal notation (not scientific).
  • setprecision(2) shows exactly 2 decimal places.
  • Example: 20 becomes 20.00, 20.5 becomes 20.50

7. Displaying the Result

The program prints: cout << "Average of " << num1 << ", " << num2 << ", and " << num3 << " = " << average << endl;

Output: Average of 10.00, 20.00, and 30.00 = 20.00

This clearly shows the input numbers and the calculated average.


8. Understanding the Average Formula

Mathematical Definition:

Average = (Sum of all numbers) / (Count of numbers)

For three numbers: Average = (a + b + c) / 3

Properties:

  • The average is always between the minimum and maximum values.
  • If all numbers are equal, the average equals that number.
  • The average represents the "center" of the data.

Example Calculation:

Numbers: 10, 20, 30

  • Sum: 10 + 20 + 30 = 60
  • Count: 3
  • Average: 60 / 3 = 20

9. Other Methods (Mentioned but not shown in code)

Method 2: Using Functions

float calculateAverage(float a, float b, float c) { return (a + b + c) / 3.0; }

  • Encapsulates calculation in a function.
  • Makes code reusable and modular.

Method 3: Using Arrays

float numbers[3]; float sum = 0; for (int i = 0; i < 3; i++) { cin >> numbers[i]; sum += numbers[i]; } float average = sum / 3.0;

  • Uses array to store numbers.
  • More flexible for handling multiple numbers.

Method 4: Using Templates

template<typename T> T average(T a, T b, T c) { return (a + b + c) / 3.0; }

  • Template function works with different data types.
  • More generic and reusable.

10. When to Use Each Method

  • Basic Formula: Best for learning - simple and straightforward.

  • Functions: Best for code organization - reusable and modular.

  • Arrays: Good for handling many numbers - scalable approach.

  • Templates: Best for generic code - works with different types.

Best Practice: Use the basic formula for simple cases, or functions for better code organization.


11. Important Considerations

Data Type Selection:

  • Use float or double for decimal numbers.
  • Use int only if you're certain all inputs are integers.
  • float is sufficient for most cases, double for higher precision.

Division by Zero:

  • This program divides by 3, which is never zero.
  • For variable count, check if count > 0 before dividing.

Precision:

  • Floating-point arithmetic may have precision limitations.
  • For exact calculations, consider using appropriate precision.

12. Common Use Cases

Statistics:

  • Calculating mean values.
  • Data analysis and reporting.
  • Statistical computations.

Programming Practice:

  • Basic arithmetic operations.
  • Understanding floating-point division.
  • Input/output formatting.

Real-World Applications:

  • Grade point averages.
  • Temperature averages.
  • Financial calculations.

13. return 0;

This ends the program successfully.


Summary

  • Average is calculated by summing numbers and dividing by count.
  • For three numbers: (a + b + c) / 3.0
  • Use 3.0 (not 3) to ensure floating-point division.
  • Format output with fixed and setprecision for decimal display.
  • Understanding averages is essential for statistics and data analysis.
  • Multiple methods exist: basic formula, functions, arrays, templates.
  • Choose method based on needs: simplicity vs. code organization vs. scalability.

This program is fundamental for beginners learning arithmetic operations, understanding floating-point division, formatting output, and preparing for statistical calculations in C++ programs.