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

Recommended Answers

All 3 Replies

Store your input in an Array, or better yet a generic List.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.