Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Average of 3 Numbers in C++

Average three numbers in C++ with float input and divide by 3.0, then print with two decimals. Explains why dividing by 3 with ints loses the fraction.

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}

Sample Output

Average of 10.00, 20.00, and 30.00 = 20.00

Inputs and result formatted to two decimals.

In this one

  • How to read three values in one cin statement
  • Why parentheses are needed around the sum
  • Why 3.0 forces floating-point division
  • How to format with fixed and setprecision

How it works

  1. 1

    Read 10 20 30.

  2. 2

    Sum = 60.

  3. 3

    Divide by 3.0 → 20.

  4. 4

    Print all values with two decimals.

Edge cases

Missing parentheses

Write num1 + num2 + num3 / 3.0.

Only num3 is divided, giving 10 + 20 + 10 = 40. Parentheses fix the order.

Integer version

Use int variables and divide by 3.

10 + 20 + 31 = 61, 61 / 3 = 20, losing the .33.

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:

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.

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:

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

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.

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

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.

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.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is (1 + 2 + 2) / 3 in int math?

Pick an answer to check it.

Question 2

num1 + num2 + num3 / 3.0 with 3, 3, 3 gives:

Pick an answer to check it.

Questions people ask

Why 3.0 instead of 3?

The floats already make the division floating-point, but 3.0 makes the intent obvious and protects you if the types change to int.

How do I average n numbers?

Sum in a loop and divide by n, as in Array Average.

Why does 10 print as 10.00?

fixed with setprecision(2) applies to every float printed after it.