140k views
4 votes
Write a single statement that assigns avg_sales with the average of num_sales1, num_sales2, and num_sales3. Sample outputs with: 3 4 8

Average sale: 5

User Gerd K
by
7.1k points

1 Answer

6 votes

Here is an example of a single statement that assigns the variable avg_sales with the average of num_sales1, num_sales2, and num_sales3 in Java:

avg_sales = (num_sales1 + num_sales2 + num_sales3) / 3.0;

This statement uses the addition operator (+) to sum the values of num_sales1, num_sales2, and num_sales3, and then divides the result by 3 using the division operator (/) to find the average. The division operator is used with a floating-point number (3.0) to ensure that the result is also a floating-point number.

For example, if num_sales1 = 3, num_sales2 = 4, num_sales3 = 8, the statement

avg_sales = (num_sales1 + num_sales2 + num_sales3) / 3.0;

will assign the value 5.0 to avg_sales.

User Cgp
by
6.9k points