In this tutorial, you will learn how to write a C++ program to find the average of three numbers. We shall cover the average formula, user input with cin, and the difference between integer and floating-point division.

C++ Program to Find Average of Three Numbers

To find the average of three numbers, add the three values and divide their sum by 3.

</>
Copy
average = (number1 + number2 + number3) / 3

For example, if the numbers are 10, 20, and 30, their sum is 60, and the average is 60 / 3 = 20.

We shall read three numbers from user and compute the average. Formula to compute the average of three numbers is given below.

C++ Program - Average of Three Numbers

Algorithm to find the average of three numbers in C++

We shall use following algorithm to compute average of three numbers in C++.

  1. Start.
  2. Read first number to num_1.
  3. Read second number to num_2.
  4. Read third number to num_3.
  5. Initialize average with (num_1 + num_2 + num_3) /3.
  6. Stop.

When the average can contain a fractional part, make sure the division is performed as floating-point division. For example, divide by 3.0 instead of 3.

C++ program to find average of three integer inputs

In this example, we shall implement the above algorithm in C++. We shall read the numbers using cin, and compute the average using C++ Addition and C++ Division.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int num_1;
   cout << "Enter num_1 : ";
   cin >> num_1;

   int num_2;
   cout << "Enter num_2 : ";
   cin >> num_2;

   int num_3;
   cout << "Enter num_3 : ";
   cin >> num_3;
   
   float average = (num_1 + num_2 + num_3) / 3;
   cout << "Average : " << average << endl;
}

The variable average is declared as float, which can store a fractional result. However, the expression on the right still divides integers by the integer literal 3. That distinction matters when the sum is not exactly divisible by 3.

Output

Enter num1 : 23
Enter num2 : 41
Enter num3 : 35
Average : 33

For these sample values, the sum is 99, which is exactly divisible by 3, so the program prints 33. However, there is an important detail in the expression used above: all operands in (num_1 + num_2 + num_3) / 3 are integers. C++ therefore performs integer division before assigning the result to float. If the result is not a whole number, its fractional part is discarded.

Use 3.0 to preserve decimal values in the C++ average

Storing the result in float or double is not enough by itself. At least one operand of the division should be floating-point. A simple way is to divide by 3.0.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int num_1 = 10;
    int num_2 = 20;
    int num_3 = 21;

    double average = (num_1 + num_2 + num_3) / 3.0;

    cout << "Average : " << average;
    return 0;
}

The sum is 51. Floating-point division calculates 51 / 3.0, so the fractional result is retained.

Average : 17

Here the result is still a whole number. Consider inputs 10, 20, and 22. Their sum is 52, so the average is approximately 17.3333. With integer division by 3, the expression would instead produce 17.

C++ average of three numbers with decimal input

If the three input values themselves may contain decimal parts, store them as double and calculate their average as a double.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    double num_1, num_2, num_3;

    cout << "Enter three numbers: ";
    cin >> num_1 >> num_2 >> num_3;

    double average = (num_1 + num_2 + num_3) / 3.0;

    cout << "Average : " << average;
    return 0;
}

For example, if the user enters 12.5, 15.0, and 17.5, the sum is 45 and the average is 15.

Enter three numbers: 12.5 15.0 17.5
Average : 15

Average calculation with variables in C++

The three values do not have to come from keyboard input. If they are already stored in variables, the same formula applies.

</>
Copy
double a = 6.5;
double b = 8.0;
double c = 10.5;

double average = (a + b + c) / 3.0;

The parentheses are useful because they make the intended order clear: first add all three values, then divide the sum by 3.0.

Integer division mistake when finding an average in C++

A common mistake is to expect a decimal result simply because the destination variable is declared as float or double.

</>
Copy
double average = (10 + 20 + 22) / 3;

The division above is evaluated using integers, so 52 / 3 produces 17 before that value is converted to double. Use a floating-point divisor instead.

</>
Copy
double average = (10 + 20 + 22) / 3.0;

This produces approximately 17.3333. The same effect can be obtained by explicitly converting one operand to a floating-point type.

</>
Copy
double average = static_cast<double>(num_1 + num_2 + num_3) / 3;

C++ average of three numbers summary

In this C++ Tutorial, we have written an algorithm and C++ programs to find the average of three numbers. The calculation is (a + b + c) / 3. When a fractional result is possible, use floating-point division, such as division by 3.0, so that C++ does not discard the fractional part during integer division.

C++ average of three numbers editorial QA checklist

  • Verify that each average example adds exactly three values before dividing by three.
  • Check whether integer inputs can produce a fractional average and use floating-point division where required.
  • Confirm that examples using float or double do not accidentally perform integer division first.
  • Verify that every displayed output matches the values and expression used in its corresponding C++ program.
  • Check that examples accepting decimal input store those inputs in a floating-point type such as double.