Average of 3 Numbers
Average of 3 Numbers in C++ (4 Programs)
C++ Average of 3 Numbers Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}Enter three numbers: 10 20 30 Average of 10.00, 20.00, and 30.00 = 20.00
Understanding Average of 3 Numbers
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:
The average represents the central value of a set of numbers, providing a single value that summarizes the data.
Example:
---
2. Header Files Used
---
3. Declaring Variables
The program declares:
float num1, num2, num3;
---
4. Taking Input From the User
The program asks:
cin >> num1 >> num2 >> num3;
The >> operator can chain multiple inputs, reading them sequentially.
---
cout << "Enter three numbers: ";5. Calculating the Average
The calculation is done using:
float average = (num1 + num2 + num3) / 3.0;
How it works
:
Why 3.0 instead of 3?
:
---
6. Formatting the Output
To display the result with 2 decimal places:
---
cout << fixed << setprecision(2);7. 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.
---
cout << "Average of " << num1 << ", " << num2 << ", and " << num3 8. 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
:
---
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;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;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
:
Division by Zero
:
Precision
:
---
12. Common Use Cases
Statistics
:
Programming Practice
:
Real-World Applications
:
---
13. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning arithmetic operations, understanding floating-point division, formatting output, and preparing for statistical calculations in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Average of 3 Numbers
This C++ program is part of the "Array Operations Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.