0

I just want only the average. I already did the sum.

import java.util.Scanner;

public class inbetweennumber {
    public static void main(String args []){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the 1st number");
        int num1 = input.nextInt();
        System.out.println("Enter the 2nd number");
        int num2 = input.nextInt();
        int sum = 0;
        double avg;

        for (int i = num1 + 1; i < num2; i++) {
            sum += i;
        }
        System.out.println("Sum: " + sum);
        avg = sum / num2;
        System.out.println("Average: " + avg);
    }
    
}

This not working for me. Example session:

Enter the 1st number
5
Enter the 2nd number
10
Sum: 30
Average: 3.0

The sum is correct: 6 + 7 + 8 + 9 = 30. The average should be 7.5, but comes out as 3.0.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    what does "not work' means ? integer division does not work well – azro Dec 30 '22 at 09:36
  • 1
    Does this answer your question? [Int division: Why is the result of 1/3 == 0?](https://stackoverflow.com/questions/4685450/int-division-why-is-the-result-of-1-3-0) – f1sh Dec 30 '22 at 09:44
  • If I enter 5 and 9, then the numbers in between are 6, 7 and 8, and their sum is 21. Do you want the average to be 7? You are doing 21 / 9 (because `num2` is 9). That doesn’t look right to me. A possible trick is: the average of the numbers will also be the average of the two numbers entered, so you may just do `(num1 + num2) / 2.0`. – Ole V.V. Dec 30 '22 at 09:53
  • Welcome to Stack Overflow. One of the first lessons to learn here is: When asking about code that isn’t working, always give expected output and precisely how observed output differs, In this case you will first need to give the example input you are using. – Ole V.V. Dec 30 '22 at 09:55
  • 1
    the average is just `(num1+num2)/2.0` and (consequently) the sum is `(num1+num2)*(num2-num1-1)/2` (if `num2 > num1+1`) – user16320675 Dec 30 '22 at 10:02
  • @f1sh I would say that it partly does. The OP has got one more problem in the math. Your lnk will certainly be helpful to the OP. – Ole V.V. Dec 30 '22 at 10:03
  • Between num1 and num2 are (num2 - num1 - 1) numbers (excluding num1 and num2). So after converting `sum` to `double` divide by this count. – Ole V.V. Dec 30 '22 at 10:31

1 Answers1

2

your average calculator is wrong. Please change it look like this;

avg =(double) sum / (num2-num1-1);
nietzsche
  • 21
  • 3
  • Thanks for the contribution. It's fully correct. You may want to add some explanation what went wrong in the original code or why the code should be written in this way. I like how you address both problems in the question, nice. – Ole V.V. Dec 30 '22 at 21:16
  • Thanks for your feedback. You're right. The real problem is that it is important whether or not the figures taken to calculate the mean are included in the for. Since the last number "num2", which is the last number in the condition in the for, was not included in the transaction, the total number entered in the transaction was overestimated by 1. – nietzsche Feb 03 '23 at 12:59