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;

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);

	}
}

Recommended Answers

All 6 Replies

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

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.

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;

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).

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)

Thanks for your help :)

I'm glad I understand it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.