Programming Challenge Chapter 3 – Q #4 Average Rainfall – Tony Gaddis – Starting Out With C++
Problem: -
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
- Use string header file for string variables.
- Declare three string variable for month name.
- Declare three int variables for rainfall.
- Declare float variable for average.
- Take input of first month name and rainfall from the user by using cin.
- Take input of second month name and rainfall from the user by using cin.
- Take input of three month name and rainfall from the user by using cin.
- Calculate average of three month rainfall.
- In last, display Name and Average output on the screen.
- Return 0 to the main function.
Also, I attach CPP file of this problem. You can download this file
0 Comments