C++ Program to Find Sum and Average of Two Numbers

In this post, we will learn how to find the sum and average of two numbers using the C++ Programming language.

This program asks the user to enter two numbers, then it calculates the sum of two numbers using the (+) arithmetic operator and average using the formula: Average = Sum / No. of terms.

So, without further ado, let’s begin this post.

C++ Program to Find Sum and Average of Two Numbers

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

int main(){
    int num1, num2, sum;
    float avg;
    
    // Asking for input
    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;
    
    sum = num1 + num2;
    
    // Finding average 
    avg = sum / 2;
    
    // Displaying output
    cout << "Sum of two numbers: " << sum << endl;
    cout << "Average of two numbers: " << avg << endl;
    return 0;
}

Output

Enter the first number: 25
Enter the second number: 35
Sum of two numbers: 60
Average of two numbers: 30

How Does This Program Work?

    int num1, num2, sum;
    float avg;

In this program, we have declared three integer data type variables and one floating data type variable named num1, num2, sum, and avg respectively.

    // Asking for input
    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;

The user is asked to enter two numbers to find the sum and average.

    sum = num1 + num2;

Sum of two numbers is calculated using the (+) arithmetic operator.

    // Finding average 
    avg = sum / 2;

Similarly, average is calculated using the formula:

  • Average = Total Sum / Total no. of terms
    // Displaying output
    cout << "Sum of two numbers: " << sum << endl;
    cout << "Average of two numbers: " << avg << endl;

The sum and average of two numbers are displayed on the screen using the cout statement.

Conclusion

I hope after going through this post, you understand how to find the sum and average of two numbers using the C++ Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *