Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 .NET4
           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;
               }
 
           }
 
           Console.ReadLine();
Posted 2-Mar-13 18:02pm
v4
Rate this: bad
good
Please Sign up or sign in to vote.

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[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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;
           }
 
           Console.ReadLine();
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...
  Permalink  
v3
Comments
Allwin456 at 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 at 2-Mar-13 23:58pm
   
Its solved now. Thanks
Tadit Dash at 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 at 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)

  Print Answers RSS
0 OriginalGriff 341
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 220
3 Praneet Nadkar 197
4 Shweta N Mishra 161
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 3 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100