C++ Program to Find the Sum and Average of Three Numbers

Howdy readers, today you will learn how to write a C++ program to find the sum and average of three numbers.

The below program reads three integers from the user and computes their sum and average using the following formulas: –

  • Sum = Number1 + Number2 + Number3
  • Average = (Number1 + Number2 + Number3) / 3

So, without any delay, let’s begin this tutorial.

C++ Program to Find the Sum and Average of Three Numbers

C++ Program

// C++ Program to Find the Sum and Average of Three Numbers
#include <iostream>
using namespace std;

int main(){
    int num1, num2, num3, sum;
    float average;
    
    // Taking input
    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;
    cout << "Enter the third number: ";
    cin >> num3;
    
    // Sum of three numbers
    sum = num1 + num2 + num3;
    
    // Average of three numbers
    average = sum / 3;
    
    // Display result
    cout << "The sum of three numbers is: " << sum << endl;
    cout << "The average of three numbers is: " << average << endl;
    
    return 0;
}

Output

Enter the first number: 12
Enter the second number: 15
Enter the third number: 21
Sum of three numbers is: 48
Average of three numbers is: 16
C++ Program to Find the Sum and Average of Three Numbers

Explanation

    int num1, num2, num3, sum;
    float average;

We have declared four int data type variables and one float data type variable num1, num2, num3, sum and average respectively.

    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;
    cout << "Enter the third number: ";
    cin >> num3;

Then, the user is asked to input three integers. These numbers get stored in the num1, num2 and num3 variables.

    sum = num1 + num2 + num3;

The sum of three numbers is computed with the help of (+) plus operator. The result gets stored in the sum named variable.

    average = sum / 3;

The average of three numbers is calculated using the formula:

  • Average of Three Numbers = Sum / 3 = (num1 + num2 + num3) / 3.

The average of three numbers gets stored in the average named variable.

    cout << "The sum of three numbers is: " << sum << endl;
    cout << "The average of three numbers is: " << average << endl;

Finally, the sum and average of three numbers are displayed on the screen using the cout statement.

Conclusion

Today you learned how to write a C++ program to find the sum and average of three numbers.

If you have any queries related to the program, comment down your questions in the comment section.

Thanks for reading.

Happy Coding!!

Also Read:

Leave a Comment