Average of prime number between 1 & 100

Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Dec 2004
Posts: 38
Reputation: mindfrost82 is an unknown quantity at this point 
Solved Threads: 0
mindfrost82 mindfrost82 is offline Offline
Light Poster

Average of prime number between 1 & 100

 
0
  #1
May 17th, 2006
I read the sticky about helping with homework, so I hope this is ok.

I'm new to programming, and I think I have the logic correct, but it doesn't give me the correct answer.

We haven't learned arrays yet, so we have to do this using if statements and loops.

The program is supposed to find all the prime numbers between 1 and 100. Find the sum, then output the average.

Any help or suggestions would be appreciated. I'm not looking for the answer, just some help on what I'm doing wrong. Thanks.

  1. using System;
  2.  
  3. public class PrimeNumbers
  4.  
  5. {
  6. // Main method entry point
  7. public static void Main()
  8.  
  9. {
  10. // declare variables
  11. // n starts at 2 as the first prime number
  12. // totalPrimeNumbers starts at 1 since 2 is a prime number
  13. // sumOfPrimes starts at 2 since 2 is the first prime number
  14. int n = 2, totalPrimeNumbers = 1, x;
  15. double sumOfPrimes = 2, average;
  16.  
  17. // while loop when n <= 100
  18. while (n <= 100)
  19. {
  20. // test if n is prime
  21. for (x = 2; x < n; x++)
  22. {
  23. if ((n % x) != 0)
  24. {
  25. sumOfPrimes = sumOfPrimes + n;
  26. totalPrimeNumbers++;
  27. // change value of x to end for loop
  28. x = n + 1;
  29. }
  30. }
  31. // increase n by 1 to test next number
  32. n++;
  33. }
  34. // calculate average
  35. average = sumOfPrimes / totalPrimeNumbers;
  36. // display average
  37. Console.WriteLine("The average of all prime numbers between 1 and 100 is: {0}", average);
  38.  
  39. }
  40. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Average of prime number between 1 & 100

 
0
  #2
May 18th, 2006
Hi,
We don't mind helping with homework, we just hate when someone does absolutely nothing and just wants someone to do their homework for them.
What you posted is fine.

A couple of hints to get you going in the write direction.
1) the way you have it set up, all odd numbers are prime
2) use a bool called IsPrime and set it to true at the very beginning of the outer loop


let me know if you need more help
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 38
Reputation: mindfrost82 is an unknown quantity at this point 
Solved Threads: 0
mindfrost82 mindfrost82 is offline Offline
Light Poster

Re: Average of prime number between 1 & 100

 
0
  #3
May 18th, 2006
Originally Posted by campkev
Hi,
We don't mind helping with homework, we just hate when someone does absolutely nothing and just wants someone to do their homework for them.
What you posted is fine.

A couple of hints to get you going in the write direction.
1) the way you have it set up, all odd numbers are prime
2) use a bool called IsPrime and set it to true at the very beginning of the outer loop


let me know if you need more help
I'm not sure I understand why I would need the bool variable IsPrime.

I think the way I have my If statement setup will work, its just not calculating the prime numbers correctly.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Average of prime number between 1 & 100

 
0
  #4
May 18th, 2006
it won't work, because it is going to say the number is prime the first time it finds a number that it is not divisible by.

set the bool IsPrime to true, then go through the inner loop, if you find a number that it IS divisible by, you set it to false and exit the inner loop.
when you are done with the inner loop, if IsPrime is true, then do sumOfPrimes = sumOfPrimes + n;
totalPrimeNumbers++;

also, to get out of a loop, you normally just use break;
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 38
Reputation: mindfrost82 is an unknown quantity at this point 
Solved Threads: 0
mindfrost82 mindfrost82 is offline Offline
Light Poster

Re: Average of prime number between 1 & 100

 
0
  #5
May 18th, 2006
  1. using System;
  2.  
  3. public class PrimeNumbers
  4.  
  5. {
  6. // Main method entry point
  7. public static void Main()
  8.  
  9. {
  10. // declare variables
  11. int n = 2, totalPrimeNumbers = 0, x;
  12. double sumOfPrimes = 0, average;
  13.  
  14. // while loop when n <= 100
  15. while (n <= 100)
  16. {
  17. bool isPrime = true;
  18.  
  19. // test if n is prime
  20. for (x = 2; x < n; x++)
  21. {
  22. if ((n % x) == 0)
  23. {
  24. isPrime = false;
  25. break;
  26. }
  27. }
  28.  
  29. if (isPrime == true)
  30. {
  31. sumOfPrimes = sumOfPrimes + n;
  32. totalPrimeNumbers++;
  33. }
  34. n++;
  35. }
  36. average = sumOfPrimes / totalPrimeNumbers;
  37. Console.WriteLine("The average of all prime numbers between 1 and 100 is: {0}", average);
  38.  
  39. }
  40. }

This is what I have now and it appears to work. I get 42.4 as the average (I think that's right).
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Average of prime number between 1 & 100

 
0
  #6
May 18th, 2006
looks right to me. It also looks like you caught that you were counting 2 twice. Very good.

Also, minor detail but
if(IsPrime == true)
is kind of redundant
you can just use
if(IsPrime)
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 38
Reputation: mindfrost82 is an unknown quantity at this point 
Solved Threads: 0
mindfrost82 mindfrost82 is offline Offline
Light Poster

Re: Average of prime number between 1 & 100

 
0
  #7
May 18th, 2006
Thanks for your help

I'm glad I understand it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC