# [SOLVED][Beginner] Arithmetic Question, Why does product become negative after a certain point?

• August 27th, 2013, 11:15 PM
beibixx
[SOLVED][Beginner] Arithmetic Question, Why does product become negative after a certain point?
Here is the problem:
Write an application that inputs three integers from the user and displays the sum, average, product, smallest, and largest of the numbers. Note: Calculation of average in this exercise should result in an integer representation of average without repeating decimals.

Here is my code:
Code :

```//Exercise 2.17 from Deitel // Write an application that inputs three integers and displays sum, average, product, smallest, and largest of numbers //Average calculations should not have a repeating decimal   import java.util.Scanner; //Java utilities package import statement   public class Average { //main method for application public static void main (String [] args) { Scanner input = new Scanner(System.in); //Scanner input from user   int number1, number2, number3, sum, average, product, smallest, largest;   System.out.print( "Enter the first integer: " ); //prompt for number1 number1 = input.nextInt();   System.out.print( "Enter the second integer: " ); //prompt for number2 number2 = input.nextInt();   System.out.print( "Enter the third integer: " ); //prompt for number3 number3 = input.nextInt();   sum = number1 + number2 + number3;   average = (number1 + number2 + number3) / 3;   product = number1 * number2 * number3;   System.out.printf( "Sum = %d \nAverage = %d \nProduct = %d \n", sum, average, product );   if (number1 < number2 && number1 < number3) //if number1 is smallest System.out.println( number1 + " is smallest");   if (number2 < number1 && number2 < number3) //if number 2 is smallest System.out.println( number2 + " is smallest");   if (number3 < number1 && number3 < number2) //if number3 is smallest System.out.println( number3 + " is smallest");   if (number1 > number2 && number1 > number3) //if number1 is largest System.out.println( number1 + " is largest");   if (number2 > number1 && number2 > number3) //if number 2 is largest System.out.println( number2 + " is largest");   if (number3 > number1 && number3 > number2) //if number3 is largest System.out.println( number3 + " is largest");   } //end main method } //end Average class```

When I run the program with smaller numbers like 60, 30, 50 then the program runs fine. Once
I start entering 349284, 409823409, 123980128 as my integers then I get a negative product. I wish I could provide the cut off point for when I start seeing the negative product printed but I don't actually know. I tested 100,000 for all three integers and the product was negative again so I'm assuming anything greater than 100,000.

This maybe silly question :o but please let me know if I'm doing something wrong. Thanks!!

Also! I'm new please let me know if this question should be posted in a different section or on a different forum in general.
• August 27th, 2013, 11:31 PM
Junky
Re: [Beginner] Arithmetic Question, Why does product become negative after a certain point?

2's compliment
Overflow/underflow
• August 27th, 2013, 11:33 PM
jps
Re: [Beginner] Arithmetic Question, Why does product become negative after a certain point?
Quote:

Originally Posted by beibixx
I wish I could provide the cut off point for when I start seeing the negative

An int can only hold a value so big
Code :

```System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MAX_VALUE + 1);```
Quote:

Originally Posted by beibixx
This maybe silly question :o but please let me know if I'm doing something wrong. Thanks!!

Nothing wrong with undirected discovery. Hit up your favorite search engine and see what you can find about it
• August 27th, 2013, 11:38 PM
helloworld922
Re: [Beginner] Arithmetic Question, Why does product become negative after a certain point?
int's are signed 32 bit integers (more specifically, two-compliment signed 32-bit integers). That means the largest value they can hold without overflowing is 2^31-1, or 2147483647.

Because of the way integer math works on computers results get truncated if the operation overflows. However, the sign bit coincidentally is what would be the most significant bit so you end up with some odd-ball negative numbers.

There are ways to estimate/check pre-emptively if a given arithmetic operation will overflow or not, but for this assignment I probably wouldn't be worried about it unless your teacher explicitly says you need to handle this.

You could try switching to use longs. These are 64-bit signed integers, and therefore can hold numbers up and including 2^63-1, or 9223372036854775807, or alternatively use the BigInteger class which has arbitrary precision.
• August 28th, 2013, 12:28 AM
beibixx
Re: [Beginner] Arithmetic Question, Why does product become negative after a certain point?
Thanks guys! I will also Google :cool: