13,048,846 members (78,451 online)
Rate this:
See more:
```           int i, j, count =0;

for (i=2; i<=100; i++)
{
for (j=1; j<=100; j++)

{
if (i % j == 0)
{
count++;

}
}

if (count == 2)
{
Console.WriteLine("This is a prime number" + i);
count = 0;
}

}

Posted 2-Mar-13 17:02pm
Updated 2-Mar-13 17:25pm
v4

Rate this:

## Solution 2

You only reset count if the current count is two, but you should always reset it.

And you only need to check up to the square root of the number you are checking.

And look into http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes[^]
Rate this:

## Solution 1

```int i, j, count =0;

for (i=2; i<=100; i++)
{
for (j=1; j<=i; j++)

{
if (i % j == 0)
{
count++;
}
}

if (count == 2)
{
Console.WriteLine("This is a prime number" + i);
}

// This should be after the if, as we need to make it 0 every time.
count = 0;
}

```

1. You should have the second loop condition like `j<=i` and not `j<=100`.
Actually you need to check whether the number is divisible with 1, 2.... upto the number you are comparing with (that is i).

2. Count should be 0 every time, but you are making it 0 when it is prime, inside the if condition.
So, 2, 3 are prime, and when 4 comes it does not make count 0, hence stops.

Thanks...
v3
Allwin456 2-Mar-13 23:52pm

@Tadit Dash .. But still then the out is --> This is a prime number 2
This is a prime number 3

And not showing the output for any other numbar. What might be the reason ??
Allwin456 2-Mar-13 23:58pm

Its solved now. Thanks
Tadit Dash 3-Mar-13 0:10am

Hey I am sorry...
I could not check it at once...
I have updated the answer, check it out.

You have to make count to 0 every time.
Allwin456 3-Mar-13 20:47pm

ok. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard MacCutchan 114 Satya Prakash Swain 114 OriginalGriff 105 Sheila Pontes 85 Jochen Arndt 85
 OriginalGriff 4,963 RickZeeland 1,924 ppolymorphe 1,753 F-ES Sitecore 1,553 Dave Kreskowiak 1,379

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 3 Mar 2013