Reputation: 1
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.
Upvotes: 0
Views: 92
Reputation: 31
your average calculator is wrong. Please change it look like this;
avg =(double) sum / (num2-num1-1);
Upvotes: 2