Hello,

Do anyone know how to continue the loop to get an average of entered numbers:

//Using loop to enter the numbers 
            for (int i = 1; i <= 10; i++)
            {
                //Entering ten numbers
                Console.Write("Enter the number:");
                int Number = int.Parse(Console.ReadLine());
            
                /*If user write number 0 in console before he conclude the 10th number,
                program will calculate just average of entered numbers*/
                if (Stevilo == 0)
                    break;
            }
.
.
.

Thanks for any help

Regards,
bisiii

Edited 5 Years Ago by __avd: Added [code] tags. Do wrap your programming code blocks within [code] ... [/code] tags

Hello,

Do anyone know how to continue the loop to get an average of entered numbers:

//Using loop to enter the numbers 
            int Number, counter = 0; // declare here.
            for (int i = 1; i <= 10; i++)
            {
                //Entering ten numbers
                Console.Write("Enter the number:");
                Number += int.Parse(Console.ReadLine()); //keep incrementing number

                /*If user write number 0 in console before he conclude the 10th number,
                program will calculate just average of entered numbers*/
                if (Stevilo == 0)
                    break;
                counter++; // Monitor the number of loops in case they break out early.
            }
.           double avg = Number / counter;
.
.

Thanks for any help

Regards,
bisiii

You need to create a variable outside the loop so it will retain it's value throughout the process. Right now you are creating the variable each time the loop iterates.

I added a couple of new lines and comments to your original code example. See the quote above.

Edited 5 Years Ago by jbrock31: n/a

You need to create a variable outside the loop so it will retain it's value throughout the process. Right now you are creating the variable each time the loop iterates.

I added a couple of new lines and comments to your original code example. See the quote above.

It works fine :D

Thank you so much for your help, jbrock31. Because of your helpful comments, now I really understand these loops.
Thanks, because you took some time for this and help me.

This question has already been answered. Start a new discussion instead.