w3resource

C++ Exercises: Compute the total and average of four numbers


Total and Average of Four Numbers

Write a C++ program to compute the total and average of four numbers.

Visual Presentation:

C++ Exercises: Compute the total and average of four numbers

Sample Solution:

C++ Code :

#include <iostream> // Including input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function
{
    float n1, n2, n3, n4, tot, avrg; // Declaring float variables for four numbers, total, and average

    cout << "\n\n Compute the total and average of four numbers :\n"; // Outputting a message indicating the purpose of the program
    cout << "----------------------------------------------------\n"; // Outputting a separator line

    cout << " Input 1st two numbers (separated by space) : "; // Prompting the user to input the first two numbers
    cin >> n1 >> n2; // Taking input for the first two numbers from the user

    cout << " Input last two numbers (separated by space) : "; // Prompting the user to input the last two numbers
    cin >> n3 >> n4; // Taking input for the last two numbers from the user

    tot = n1 + n2 + n3 + n4; // Calculating the total of all four numbers
    avrg = tot / 4; // Calculating the average of all four numbers

    cout << " The total of four numbers is : " << tot << endl; // Displaying the calculated total
    cout << " The average of four numbers is : " << avrg << endl; // Displaying the calculated average
    cout << endl; // Outputting a blank line for better readability

    return 0; // Returning 0 to indicate successful program execution
} // End of the main function

Sample Output:

 Compute the total and average of four numbers :                       
----------------------------------------------------                   
 Input 1st two numbers (separated by space) : 25 20                    
 Input last two numbers (separated by space) : 15 25                   
 The total of four numbers is : 85                                     
 The average of four numbers is : 21.25   

Flowchart:

Flowchart: Compute the total and average of four numbers

For more Practice: Solve these Related Problems:

  • Write a C++ program that calculates the total and average of four numbers using an array and a loop.
  • Write a C++ program to compute the sum and average of four numbers with error checking for non-numeric inputs.
  • Write a C++ program that accepts four numbers, calculates their sum, and then displays the average formatted to two decimal places.
  • Write a C++ program that reads four numbers from the user, computes both the total and the average, and then prints a detailed summary of the computation.

Go to:


PREV : Quotient and Remainder Calculation.
NEXT : Rectangular Pattern of Single Digit.

C++ Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.