Average of 3 Numbers

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

BeginnerTopic: Array Operations Programs
Back

C++ Average of 3 Numbers Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#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;
}
Output
Enter three numbers: 10 20 30
Average of 10.00, 20.00, and 30.00 = 20.00

Understanding Average of 3 Numbers

This program demonstrates 4 different methods to calculate average of 3 numbers: basic formula, using functions, using arrays, and using templates.

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.

Table of Contents