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.
Comments (35)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
ajinkya bharate
Level 8 , Pune, India
25 May, 11:01
make sum variable a double
Level 5 , Wyken, United Kingdom
21 February, 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
Level 11 , Banja Luka, Bosnia and Herzegovina
20 February, 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.
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 {
        BufferedReader reader = new BufferedReader(new InputStreamReader(;
        double sumNum = 0.0;
        double count = 0.0;
        double meanNum = sumNum / count;

            int number = Integer.parseInt(reader.readLine());
            if(number == -1){
            } else {
                sumNum += (number + 0.0);

Toni Fields
Level 12 , Jacksonville, United States
24 January, 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.
Level 15 , Old Town, United States
5 April, 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
com/codegym/task/task05/task0507/ error: cannot find symbol Scanner s = new Scanner(; ^ symbol: class Scanner Why am i getting this error location: class com.codegym.task.task05.task0507.Solution
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
Please help me to identify the bug
Laurence Chadwell
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...