Average of 3 Numbers in C++

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

BeginnerArray Operations ProgramsExample 4 of 7
average-of-3-numbers.cpp
1#include <iostream>
2#include <iomanip>
3using namespace std;
4
5int main() {
6 float num1, num2, num3;
7
8 cout << "Enter three numbers: ";
9 cin >> num1 >> num2 >> num3;
10
11 float average = (num1 + num2 + num3) / 3.0;
12
13 cout << fixed << setprecision(2);
14 cout << "Average of " << num1 << ", " << num2 << ", and " << num3
15 << " = " << average << endl;
16
17 return 0;
18}

Output

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

What's going on

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

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.

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.

Taking Input From the User

The program asks:

cin >> num1 >> num2 >> num3;

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

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

1cout << "Enter three numbers: ";

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)

Formatting the Output

To display the result with 2 decimal places:

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
1cout << fixed << setprecision(2);

Displaying the Result

The program prints:

<< " = " << average << endl;

Output:

Average of 10.00, 20.00, and 30.00 = 20.00

This clearly shows the input numbers and the calculated average.

1cout << "Average of " << num1 << ", " << num2 << ", and " << num3

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

Other Methods (Mentioned but not shown in code)

Method 2: Using Functions

float calculateAverage(float a, float b, float c) {

}

Encapsulates calculation in a function.
Makes code reusable and modular.
1return (a + b + c) / 3.0;

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) {

}

Template function works with different data types.
More generic and reusable.
1return (a + b + c) / 3.0;

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.

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.

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.