![]() |
| ||
| Average of prime number between 1 & 100 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. using System; |
| ||
| Re: Average of prime number between 1 & 100 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 |
| ||
| Re: Average of prime number between 1 & 100 Quote:
I think the way I have my If statement setup will work, its just not calculating the prime numbers correctly. |
| ||
| Re: Average of prime number between 1 & 100 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; |
| ||
| Re: Average of prime number between 1 & 100 using System; This is what I have now and it appears to work. I get 42.4 as the average (I think that's right). |
| ||
| Re: Average of prime number between 1 & 100 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) |
| ||
| Re: Average of prime number between 1 & 100 Thanks for your help :) I'm glad I understand it. |
| All times are GMT -4. The time now is 4:23 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC