Programming Challenge Chapter 3 – Q #3 Test Average – Tony Gaddis – Starting Out With C++

Programming Challenge Chapter 3 – Q #3 Test Average – Tony Gaddis – Starting Out With C++

Test Average

Problem: -

Write a program that asks for five test scores. The program should calculate the average test score and display it. The number displayed should be formatted in fixed-point notation, with one decimal point of precision.

Solution: -

#include <iostream>

#include<iomanip>

using namespace std;

int main()

{

      float num1, num2, num3, num4, num5;

      float average = 0;

      cout << "Enter First Number: ";

      cin >> num1;

      cout << "Enter Second Number: ";

      cin >> num2;

      cout << "Enter Third Number: ";

      cin >> num3;

      cout << "Enter Four Number: ";

      cin >> num4;

      cout << "Enter Fifth Number: ";

      cin >> num5;

      average = (num1 + num2 + num3 + num4 + num5)/5.0;

      cout << setprecision(1) << fixed << showpoint;

      cout << "Average is " << average;

      return 0;

}

This is the solution of this question




OUTPUT OF THIS QUESTION 

Input is highlighted with yellow color.


Explanation of this Solution


  1. Declare five float variables for user input.
  2. Declare another one float variable for average calculation.
  3. Take first number input from the user by using cin.
  4. Take second number input from the user by using cin.
  5. Take  third number input from the user by using cin.
  6. Take fourth number input from the user by using cin.
  7. Take fifth number input from the user by using cin.
  8. Calculate average of five numbers. 
  9. Use setprecision for one decimal notation.
  10.  In last, display Average output on the screen.
  11. Return 0 to the main function.

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

Click Here to Download This File




Thanks!  

Post a Comment

0 Comments