0

I created a Java program that accepts three integers and would print the sum, product, the smallest and largest, and the average of these three integers.

import java.util.Scanner;

public class ThreeNums
{
    public static void main (String[] args)
    {
        new ThreeNums();
    }

    public ThreeNums ()
    {
        int num1, num2, num3, sum, smallest, largest, exitPrompt = 0;
        float average;

        Scanner scan = new Scanner (System.in);

        while (exitPrompt == 0)
        {
            System.out.printf ("\n\nPlease enter three integers.\n");
            System.out.printf ("\t1st integer: ");
            num1 = scan.nextInt();
            System.out.printf ("\t2nd integer: ");
            num2 = scan.nextInt();
            System.out.printf ("\t3rd integer: ");
            num3 = scan.nextInt();
            System.out.printf ("\n");

            sum = num1+num2+num3;
            System.out.println ("Sum: " + sum);
            System.out.printf ("Product: %d\n", num1*num2*num3);

            if (num1<num2)
                smallest = num1;
            else
                smallest = num2;

            if (smallest<num3)
                smallest = smallest;
            else
                smallest = num3;

            if (num1>num2)
                largest = num1;
            else
                largest = num2;

            if (largest>num3)
                largest = largest;
            else
                largest = num3;

            System.out.printf ("Smallest integer: %d\n", smallest);
            System.out.printf ("Largest integer: %d\n", largest);

            average = sum/3;
            System.out.printf ("Average: %.3f\n", average);

            System.out.printf ("\nExit? 0 if NO, any other digit if YES\n");
            exitPrompt = scan.nextInt();
        }

        System.out.printf ("\n\n");
    }
}

Everything is okay except (I think) at lines 56-57:

average = sum/3;
System.out.printf ("Average: %.3f\n", average);

It is supposed to print the average of the three integers the user will input. For example, if the user inputs 5, 7, and 7, it is supposed to show that the average is 6.333:

Please enter three integers.
1st integer: 5
2nd integer: 7
3rd integer: 7

Sum: 19
Product: 245
Smallest integer: 5
Largest integer: 7
Average: 6.333

Exit? 0 if NO, any other digit if YES

But instead, for the average, it shows only 6.000.

.
.
.
Average: 6.000
.
.

If I input 78, 45, and 23, it only shows 48.000 instead of 48.667, and so on. What could be the problem with my code? Please help... Thanks!

Andronicus
  • 25,419
  • 17
  • 47
  • 88
  • Integer Math. You will need to add a cast or otherwise get one of the values to a float. Something like `average = (float)sum / 3;` or `average = sum / 3.0f` would work. – KevinO Mar 14 '19 at 18:16
  • You are performing an integer division, that is the result is returned as an integer since both values in the division are integers. Divide by 3.0 instead for instance – Joakim Danielson Mar 14 '19 at 18:17

1 Answers1

1

Here: average = sum/3; both sum and 3 are ints, so the result is an int and then it's conferted into float. You can overcome this with this:

average = sum / 3.0;
Andronicus
  • 25,419
  • 17
  • 47
  • 88