Question
Part 1 Given 4 integers, output their product and their average, using integer arithmetic. Ex: If the input is: 8 10 5 4 the output
Part 1
Given 4 integers, output their product and their average, using integer arithmetic.
Ex: If the input is:
8 10 5 4
the output is:
1600 6
Part 1
Given 4 integers, output their product and their average, using integer arithmetic.
Ex: If the input is:
8 10 5 4
the output is:
1600 6
My program solves it for most types. However I am getting an overflow error for large values and do not know how to resolve it.
MY CODE:************************
import java.util.Scanner;
public class ProgrammingAssignment1 { public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int int1; int int2; int int3; int int4;
int1 = scnr.nextInt(); int2 = scnr.nextInt(); int3 = scnr.nextInt(); int4 = scnr.nextInt();
//pt 1 int product = num1*num2*num3*num4; int average = (num1+num2+num3+num4)/4; System.out.println(product+" "+average); //pt 2 long product_ = ((long)num1)*num2*num3*num4;
long average_ = (long)(num1+num2+num3+num4)/4;
System.out.printf("%.3f", product_ + " " + average_); } }
*********************************
errors I am getting:
4:Part 2: Fraction not discardedkeyboard_arrow_up
0 / 2
Output differs. See highlights below. Special character legend
Input
8 10 5 4
Your output
1600 6
Expected output
1600 6 1600.000 6.750
5:Part 2: No overflowkeyboard_arrow_up
0 / 2
Output differs. See highlights below. Special character legend
Input
100000 200000 300000 500000
Your output
-1679818752 275000
Expected output
-1679818752 275000 3000000000000000000000.000 275000.000
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started