?
Solved

averages in C# programming

Posted on 2007-08-11
8
Medium Priority
?
242 Views
Last Modified: 2013-12-17
Hi Im trying to understand c# programming.

Im trying to find a code in C# programming that will find the average of all the prime numbers between 1 and 100 and prints this average on screen.  I have been trying myself but im getting nowhere.
can anyone help me with this.  im trying to figure this out by sunday thanks  
0
Comment
Question by:soundwire
  • 4
  • 3
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 19676968
what do you have already?
* calculating the prime numbers?
* totalling those numbers?
* counting those numbers?
* dividing the total by the number?
0
 

Author Comment

by:soundwire
ID: 19676979
I put in a code and it displayed all the numbers 1 through 100 but that it.  I dont know if that is th right track or not
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 19676983
so, I guess you don't have yet a function to check if a number is a prime number...
have you searched already around in the internet for just that functionality?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:soundwire
ID: 19676992
yes and came up with nothing.  
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 19677003
then, you don't search hard enough, I found several good hits on the first try...

0
 

Author Comment

by:soundwire
ID: 19677011
I tried ask.com , yahoo and google there are the 3 main engines that i get accuate info from. unless i needed to be more specific
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 1500 total points
ID: 19677013
quick google gave this ...

/// <summary>
/// This method should return whether or not an integer is a prime number
///
/// Please note that the solution should be as efficient as possible and not be breakable.
/// </summary>
/// <param name="number">Integer to be tested</param>
/// <returns>true or false</returns>
public static bool IsPrime(int number)
{
    // Throw out impossibles
    if (number < 2)
    {
        return false;
    }

    // Don't need to test above the square root of a number
    for (int i = 2; i < (number); i++)
    {
        // If remainder is 0, number is not prime
        if (number % i == 0)
        {
            // return false
            return false;
        }
    }

    // If all conditions are met, return true
    return true;
}




from there its just a matter of keeping two variables (total and count) then divide.


pseudocode

for(int i=0;i<100;i++) {
   if(IsPrime(i)) {
        total += i;
        count ++;
   }
}
Console.WriteLine("there were " + count + " primer numbers. Average value is " + total/count);

Cheers,

Greg
0
 

Author Comment

by:soundwire
ID: 19677310
thanks ill try that
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question