Answer:
Explanation:
Note: Integer division discards the fraction. Hence the average of 8, 10, 5, 4 is output as 6, not 6.75.
Note: The test cases include four very large input values whose product results in overflow. You do not need to do anything special, but just observe that the output does not represent the correct product (in fact, four positive numbers yield a negative output; wow).
Submit the above for grading. Your program will fail the last test cases (which is expected), until you complete part 2 below.
Part 2: Also output the product and average, using floating-point arithmetic.
Output each floating-point value with three digits after the decimal point, which can be achieved as follows: System.out.printf("%.3f", yourValue);
Ex: If the input is 8, 10, 5, 4, the output is:
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int num1;
int num2;
int num3;
int num4;
num1 = scnr.nextInt();
num2 = scnr.nextInt();
num3 = scnr.nextInt();
num4 = scnr.nextInt();
double average_arith = (num1+num2+num3+num4)/4.0;
double product_arith = num1*num2*num3*num4;
int result1 = (int) average_arith;
int result2 = (int) product_arith;
System.out.printf("%d %d\n",result2,result1);
System.out.printf("%.3f %.3f\n",product_arith,average_arith);
}
}
expected output : 1600.000 6.750