Ok, as its Sunday and I have nothing really important to do...
public ArrayList FindPrimes(int maxNumber)
{
ArrayList result = new ArrayList();
for (int i = 3; i <= maxNumber; i += 2)
if (IsPrime(i))
result.Add(i);
return result;
}
public double GetPrimesAverage(int maxNumber)
{
return CalcAverage(FindPrimes(maxNumber));
}
public bool IsPrime(int number)
{
double max = Math.Sqrt(number);
for (int i = 3; i <= max; i += 2)
if (number % i == 0)
return false;
return true;
}
public double CalcAverage(ArrayList list)
{
double sum = 0;
foreach (int number in list)
sum += number;
return sum / list.Count;
}
Generally its the the algorithm I already stated (including some optimizations I initially didnt want you to take care of :)). I tried to split up the functionality. The function names should be descriptive enough to tell you want each part is doing. Note that IsPrime gives only valid results for uneven numbers. As GetPrimesAverage gets an argument you can calculate the average for as much prime numbers as you want (cpu is the limit :) - dont test it with more than about 20 millions). To get the result for a maximum of 100 the following call had to be done: GetPrimesAverage(100) Do yourself the favor and read it carefully until you understand it. If you already know how to use a debugger it would be good if you just step through the code until you get it.