Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

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

1. ## [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:
```//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 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.  Reply With Quote

3. ## Re: [Beginner] Arithmetic Question, Why does product become negative after a certain point?

2's compliment
Overflow/underflow  Reply With Quote

4. ## Re: [Beginner] Arithmetic Question, Why does product become negative after a certain point? 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
```System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MAX_VALUE + 1);``` Originally Posted by beibixx This maybe silly question 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  Reply With Quote

5. ## 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.  Reply With Quote

6. ## Re: [Beginner] Arithmetic Question, Why does product become negative after a certain point?

Thanks guys! I will also Google   Reply With Quote

#### Tags for this Thread 