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

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


Problem: -

Write a program that calculates the average rainfall for three months. The program should ask the user to enter the name of each month, such as June or July, and the amount of rain (in inches) that fell each month. The program should display a message similar to the following:
The average rainfall for June, July, and August is 6.72 inches.

Solution: -

#include <iostream>

#include<string>

using namespace std;

int main()

{

      string m1, m2, m3;

      int  month1, month2, month3;

      float average = 0;

      cout << "Enter Name of 1st Month: ";

      cin >> m1;

      cout << "Enter Rainfall: ";

      cin >> month1;

      cout << "Enter Name of 2nd Month: ";

      cin >> m2;

      cout << "Enter Rainfall: ";

      cin >> month2;

      cout << "Enter Name of 3rd Month: ";

      cin >> m3;

      cout << "Enter Rainfall: ";

      cin >> month3;

      average = (month1 + month2 + month3) / 3.0;

      cout << "The Average Rainfall Of " << m1 << ", " << m2 << " and " << m3 << " 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. Use string header file for string variables.
  2. Declare three string  variable for month name.
  3. Declare three int variables for rainfall.
  4. Declare float variable for average.
  5. Take input of first month name and rainfall from the user by using cin.
  6. Take input of second month name and rainfall from the user by using cin.
  7. Take input of three month name and rainfall from the user by using cin.
  8. Calculate average of three month rainfall. 
  9.  In last, display Name and Average output on the screen.
  10. 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