# Arithmetic mean

• 8
• Locked
The arithmetic mean is a value that is often used in statistics. A hospital's average temperature is precisely calculated using the formula for the arithmetic mean. And now we've come to the critical part: write a program that takes numbers from the keyboard, sums them, and then calculates the mean until the user enters the number -1.
You can't complete this task, because you're not signed in.
ajinkya bharate
Level 8 , Pune, India
25 May 2021, 11:01
make sum variable a double
KARMA GURUNG
Level 5 , Wyken, United Kingdom
21 February 2021, 12:45
hint: use while loop, check the entered no. with (if). if it is -1 then, display the mean otherwise keep on adding double is the key
Sinisa
Level 11 , Banja Luka, Bosnia and Herzegovina
20 February 2021, 10:44
This task's description is either not properly described, or the proposed solution is flawed. The solution provided by the CodeGym expects user to eventually input -1. In the task's description, it is an condition, not an obligation. Hence, if the user doesn't input -1, the program returns an error, ie., never finishes. So, either change the requirements and say, not IF the user enters -1 but WHEN, or state the number of user inputs, so that the program can return an output in case the user doesn't enter -1.
Jac
Level 7 , Birmingham, United States
22 September 2020, 01:46
I'm getting NaN as an output. Any help?
``````package com.codegym.task.task05.task0507;

/*
Arithmetic mean

*/

public class Solution {
public static void main(String[] args) throws Exception {
double sumNum = 0.0;
double count = 0.0;
double meanNum = sumNum / count;

while(true){
if(number == -1){
System.out.println(meanNum);
break;
} else {
sumNum += (number + 0.0);
count++;
}
}

}
}``````
Toni Fields
Level 12 , Jacksonville, United States
24 January 2021, 02:59
Not sure if you figured this one out yet, but you should move your meanNum initialization to after your while loop. In your while loop, remove the print statement. This statement should come after the while loop and after you declare the meanNum variable. Everything else is fine.
ImDevin
Level 15 , Old Town, United States
5 April 2021, 14:51
Nan is what you get when you divide 0.0 by 0.0, as it is in line 20. Prior to line 20, there is no increase in neither of the numbers, so you have to fix the order of operations first.
8 February 2020, 19:30
ok i got the solution to the arithmetic one but i literally don't understand why. basically i was getting 2.16777777777 then i added {} after my if and closed it from my break and it worked.......
ъуъ
Level 9 , Vilnius, Lithuania
25 September 2020, 10:46
Cristea Ovidiu-Valentin
Level 22 , Sibiu, Romania
19 November 2019, 22:35
"Casting" is used to get a double from an integer division : 1. double d = (double)5 / 20; 2. double v = (double)5 / (double) 20; 3. double v = 5 / (double) 20; or implicit casting : double d = num * 1.0 / denom; Just learned that and it is useful ,.
Firdous Shaikh
Level 5 , Mumbai, India
6 November 2019, 07:26
Roman
Level 41
7 November 2019, 05:50
You should import Scanner class.
Raj Mishra Java Developer at Sopra banking softwa
17 October 2019, 10:28
I think you found the error as the task been closed ?
Lakshmanan Subramanian
Level 16 , United States
10 October 2019, 01:23
Level 7 , San Antonio, United States
22 September 2019, 02:01
It's funny how making everything a double makes a big difference, lmao!
Łukasz Szajek
Level 6 , Poznan, Poland
30 September 2019, 10:45
My thoughts exactly :D Thinking why 14/5 gives 2.0 istead of 2.8 gave me a headache...