Chapter 2 – Q #5: Average of Values – Tony Gaddis – Starting Out With C++

Chapter 2 – Q #5: Average of Values – Tony Gaddis – Starting Out With C++



Chapter 2 – Q #5: Average of Values – Tony Gaddis – Starting Out With C++


Problem:-

To get the average of a series of values, you add the values up and then divide the sum by the number of values. Write a program that stores the following values in five different variables: 28, 32, 37, 24, and 33. The program should first calculate the sum of these five variables and store the result in a separate variable named sum. Then, the program should divide the sum variable by 5 to get the average. Display the average on the screen.


Solution:-


#include <iostream>

using namespace std;

int main()

{

      int a = 28, b = 32, c = 37, d = 24, e = 33;

      int sum = a + b + c + d + e;

      double average = sum/5.0;

      cout << "Average Of These Number is " << average << endl;

      return 0;

}

This is the solution of this question

Explanation of this Solution

  1. I declare five different variables and initialize given values to all variables.
  2. I declare sum variable and add values of fiver variables.
  3. Then, I declare average variable and calculate average of 5 numbers.
  4. In last, display output on the screen.
  5. Return 0 to the main function.

Also, I attach CPP file of this problem. You can download this file

Post a Comment

0 Comments