Respuesta :
Answer:
Here is the Python program:
#prompts user to enter no of years
years = int(input('Enter the number of years: '))
months = 12 # stores no of months in a year i.e. 12
sum = 0 # stores the total inches of rainfall
if years < 1: #if input year value is less than 1
print("Invalid year") #displays invalid year message
else: #if input year value is greater than greater than 0
for year in range(years): #iterates once for every year
print('For year: ', year + 1,' ') #prints For year for each input year
for month in range(months): # iterates for each month 12 times
print('Enter the inches of rainfall for month ', month + 1,' ')
#asks user for inches of rainfall for each of 12 months
rainfall = int(input()) # takes value of rainfall inches for each month
sum = sum + rainfall # stores total rainfall inches
total_months = years * months #stores number of months
average_rainfall = sum / total_months #store average rainfall per month
print('Total inches of rainfall: ', sum) # prints total rainfall inches
print('Number of months: ', total_months) #print total months
print('Average rainfall per month: ', format(average_rainfall, '.3f'), 'inches')
#prints the value of average rainfall per month up to 3 decimal places
Explanation:
This program uses nested loops to take data from user and calculate average rainfall over a period of years. The program prompts user to enter the number of years and the outer loop iterates for each year entered by the user. For example if user entered 3 then the outer loop iterates 3 times for each year 1, year 2 and year 3. The inner loop asks user for inches of rainfall for each of the 12 months so it iterates 12 times. For example for 3 years and 12 months in each year the inner loop asks the user 36 times to enter rainfall inches. Every time the rainfall inches are given by user that value is added to the total value (sum) at every iteration. Then the total_months value is calculated by multiplying input year with months. If input year is 3 then total_months= 3 * 12 = 36. The average rainfall is calculated by dividing value of sum to value of total_months. Finally these values are displayed on output screen using print(). The program along with output is attached.



import java.util.Scanner;
import java.text.*;
public class AverageRainfall {
public static void main(String[] args) {
int years; // Variable to hold the amount of years
final int MONTHS = 12; // Constant to hold the amount of months in a year
int totalMonths = 0; // Months accumulator variable
double monthlyRain; // Holds the amount of rain in inches for each month
double totalRainfall = 0; // Holds the total rainfall
NumberFormat df = DecimalFormat.getInstance(); // Decimal formating
df.setMaximumFractionDigits(2);
// Create Scanner object
Scanner input = new Scanner(System.in);
// Prompt user for the number of years
System.out.println("Enter the number of years: ");
years = input.nextInt();
System.out.println("Enter the rainfall amount for each month ");
// Construct for-loop for each year
for(int i=0; i < years; i++){
for(int j=1; j<MONTHS+1; j++){
switch(j){
case 1:
System.out.print("January: ");
break;
case 2:
System.out.print("February: ");
break;
case 3:
System.out.print("March: ");
break;
case 4:
System.out.print("April: ");
break;
case 5:
System.out.print("May: ");
break;
case 6:
System.out.print("June: ");
break;
case 7:
System.out.print("July: ");
break;
case 8:
System.out.print("August: ");
break;
case 9:
System.out.print("September: ");
break;
case 10:
System.out.print("October: ");
break;
case 11:
System.out.print("November: ");
break;
case 12:
System.out.print("December: ");
}
monthlyRain = input.nextDouble();
totalRainfall += monthlyRain;
}
totalMonths += MONTHS;
}
double avgRainfall = totalRainfall/totalMonths; // Holds the average rainfall
// Newline1
System.out.println("");
// Display rainfall data
System.out.println("Total Months: " + totalMonths + "\nTotal Rainfall: " + df.format(totalRainfall) + " inches" +
"\nAverage Rainfall: " + df.format(avgRainfall) + " inches");
}
}
Learn More:https://brainly.com/question/14971268