fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. int main() {
  6. int numYear;
  7. float rainfall;
  8. float total=0;
  9. float averagePerMth;
  10. int numOfMonths;
  11.  
  12. cout << "Enter Number of Years.\n";
  13. cin >> numYear;
  14.  
  15. while (numYear < 1)
  16. {
  17. cout << "Only enter number of year that is 1 or greater than 1.\n";
  18. cin >> numYear;
  19. }
  20.  
  21. for (int i = 1; i<= numYear ; i++)
  22. {
  23. for (int e = 1; e <= 12 ; e++ )
  24. {
  25. cout << "Enter the rainfall for year : " << i << " and month " << e << endl;
  26. cin >> rainfall;
  27. while (rainfall < 0)
  28. {
  29. cout << "Only enter positive number for rainfall\n";
  30. cin >> rainfall;
  31. }
  32.  
  33. total +=rainfall;
  34. }
  35. }
  36.  
  37. numOfMonths = numYear * 12;
  38. averagePerMth = total / numOfMonths;
  39.  
  40. cout << "Average rainfall\n";
  41. cout << "---------------------\n";
  42. cout << "Number of months : " << numOfMonths << endl;
  43. cout << fixed << setprecision (3);
  44. cout << "Total inches of rainfall : " << total << endl;
  45. cout << "Average Rainfall per month : " << averagePerMth << endl;
  46. return 0;
  47. }
Success #stdin #stdout 0s 5284KB
stdin
1
5
5
5
5
10
10
10
10
20
20
20
20
stdout
Enter Number of Years.
Enter the rainfall for year : 1 and month 1
Enter the rainfall for year : 1 and month 2
Enter the rainfall for year : 1 and month 3
Enter the rainfall for year : 1 and month 4
Enter the rainfall for year : 1 and month 5
Enter the rainfall for year : 1 and month 6
Enter the rainfall for year : 1 and month 7
Enter the rainfall for year : 1 and month 8
Enter the rainfall for year : 1 and month 9
Enter the rainfall for year : 1 and month 10
Enter the rainfall for year : 1 and month 11
Enter the rainfall for year : 1 and month 12
Average rainfall
---------------------
Number of months : 12
Total inches of rainfall : 140.000
Average Rainfall per month : 11.667