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.
1#include <iostream>2#include <iomanip>3using namespace std;45int main() {6 float num1, num2, num3;78 cout << "Enter three numbers: ";9 cin >> num1 >> num2 >> num3;1011 float average = (num1 + num2 + num3) / 3.0;1213 cout << fixed << setprecision(2);14 cout << "Average of " << num1 << ", " << num2 << ", and " << num315 << " = " << average << endl;1617 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
Read 10 20 30.
- 2
Sum = 60.
- 3
Divide by 3.0 → 20.
- 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:
The average represents the central value of a set of numbers, providing a single value that summarizes the data.
Example:
Header Files Used
Declaring Variables
The program declares:
float num1, num2, num3;
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:
Why 3.0 instead of 3?:
Formatting the Output
To display the result with 2 decimal places:
cout << fixed << setprecision(2);
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:
Example Calculation:
Numbers: 10, 20, 30
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;
}
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;
Method 4: Using Templates
template<typename T>
T average(T a, T b, T c) {
return (a + b + c) / 3.0;
}
When to Use Each Method
Best Practice: Use the basic formula for simple cases, or functions for better code organization.
Important Considerations
Data Type Selection:
Division by Zero:
Precision:
Common Use Cases
Statistics:
Programming Practice:
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.