C# C# Basics (Retired) Perform if / else

ali raafat
ali raafat
444 Points

i need help in my Program

so i am trying to make an averager. I made a loop, at the numbers entered by the user but i need to find the user average when he types done. how can i make it . I tried to run it without finding the average but when i typed done the Program crashed.

Isna Rehman
Isna Rehman
25,837 Points

Hi ali, If you could show us your codes it would be easier to understand the issue. Just paste your codes here and we will try to help .

Steven Parker
Steven Parker
171,120 Points

Yes, please remember to show your code. If you are using the workspaces, you you can use the snapshot function in the workspace and provide the link to that.

1 Answer

Henrique Vignon
PRO
Henrique Vignon
Pro Student 6,415 Points

In order to find an average of anything you just have to divide the total sum of numbers by the number of numbers

So it would be something like this:

            string entry;
            double sum = 0.0;
            int count = 0;

            while (true)
            {
                Console.Write("Enter numbers to find their average, type \"done\" to finish: ");

                entry = Console.ReadLine();

                if (entry.ToLower() == "done")
                {
                    if (count > 0)
                    {
                        Console.WriteLine("The average is: " + sum / count);
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Nothing to average");
                        break;
                    }

                }

                sum += double.Parse(entry);
                count++;
            }

You'd have to add some checking to guarantee the person types valid numbers and whatnot, also you can add in some flair by changing the input message so like "Now type the second number", then "third number" and so forth.

But you can figure how to do that by yourself :)