| | |
Average of prime number between 1 & 100
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Dec 2004
Posts: 38
Reputation:
Solved Threads: 0
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.
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.
C# Syntax (Toggle Plain Text)
using System; public class PrimeNumbers { // Main method entry point public static void Main() { // declare variables // n starts at 2 as the first prime number // totalPrimeNumbers starts at 1 since 2 is a prime number // sumOfPrimes starts at 2 since 2 is the first prime number int n = 2, totalPrimeNumbers = 1, x; double sumOfPrimes = 2, average; // while loop when n <= 100 while (n <= 100) { // test if n is prime for (x = 2; x < n; x++) { if ((n % x) != 0) { sumOfPrimes = sumOfPrimes + n; totalPrimeNumbers++; // change value of x to end for loop x = n + 1; } } // increase n by 1 to test next number n++; } // calculate average average = sumOfPrimes / totalPrimeNumbers; // display average Console.WriteLine("The average of all prime numbers between 1 and 100 is: {0}", average); } }
•
•
Join Date: Jul 2005
Posts: 483
Reputation:
Solved Threads: 19
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
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
•
•
Join Date: Dec 2004
Posts: 38
Reputation:
Solved Threads: 0
•
•
•
•
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 think the way I have my If statement setup will work, its just not calculating the prime numbers correctly.
•
•
Join Date: Jul 2005
Posts: 483
Reputation:
Solved Threads: 19
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;
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;
•
•
Join Date: Dec 2004
Posts: 38
Reputation:
Solved Threads: 0
C# Syntax (Toggle Plain Text)
using System; public class PrimeNumbers { // Main method entry point public static void Main() { // declare variables int n = 2, totalPrimeNumbers = 0, x; double sumOfPrimes = 0, average; // while loop when n <= 100 while (n <= 100) { bool isPrime = true; // test if n is prime for (x = 2; x < n; x++) { if ((n % x) == 0) { isPrime = false; break; } } if (isPrime == true) { sumOfPrimes = sumOfPrimes + n; totalPrimeNumbers++; } n++; } average = sumOfPrimes / totalPrimeNumbers; Console.WriteLine("The average of all prime numbers between 1 and 100 is: {0}", average); } }
This is what I have now and it appears to work. I get 42.4 as the average (I think that's right).
![]() |
Similar Threads
- C++ prime number program help (C++)
- Prime Numbers - Help its for college (Visual Basic 4 / 5 / 6)
Other Threads in the C# Forum
- Previous Thread: Create .wav file with C#
- Next Thread: bitvector32 error
| Thread Tools | Search this Thread |
.net access algorithm array asp.net bitmap box broadcast c# check checkbox client combobox control conversion csharp database datagrid datagridview dataset datetime dbconnection decryption degrees design developer draganddrop drawing encryption enum eventhandlers excel file firefox focus form format forms function gdi+ grantorrevokepermissionthroughc#.net hospitalmanagementsystem image input install interface java libraries list loop marshalbyrefobject math mouseclick movingimage mysql netcfsvcutil.exe numeric operator path photoshop php picturebox pixelinversion platform polynomial post programming properties radians read regex remote remoting richtextbox server sleep socket sql statistics string stringformatting study system.servicemodel table tcpclientchannel text textbox thread time timer update usb usercontrol validation visualstudio webbrowser winforms wpf wpfc# xml






