Please do check what wrong in my code i hope everything right .. but on console its just displaying one single value o/p i.e 13 .. after i press enter key its displaying next prime upon each pres .. y it is going n like this ...
using System;
using System.Collections.Generic;
using System. Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
for (int i = 3; i < 50;i++ )
{
if (i % 2 == 0)
continue ;
if (i % 3 == 0)
continue;
if (i % 5 == 0)
continue;
if (i % 7 == 0)
continue;
if (i % 11 == 0)
continue;
Console.WriteLine(i);
}
}
}
}


VulpesRank #55 · 29.2K reputationPosted Jul 13, 2013, 8:32 AM EDT
The output is now:
VulpesRank #55 · 29.2K reputationPosted Jul 13, 2013, 8:38 AM EDT
because the cases 22, 33 and 44 will be picked up as multiples of 2,3 and 2 respectively.
SUNIL GUTTARank #1,832 · 810 reputationPosted Jul 13, 2013, 9:41 AM EDT
Well i tried using debugging i.e to check flow of code :)
actually problem is while executing when staring IF condition is satisfied i.e if(i%2) then it is going to continue then to for loop .. but the same flow is not same in case of second if statement i.e if(i%3) its not going on to continue when lets say i=9 . instead it going further for checking if conditions which are not necessary:)
Note: when i used curly braces for each continue .. debugging working fine .. ..
Why is issue ???
VulpesRank #55 · 29.2K reputationPosted Jul 13, 2013, 9:50 AM EDT
If i is divisible by 2 then the for loop continues to the next value.
If i is divisible by 3, 5 or 7 (but not 3,5 or 7 themselves) then the for loop continues to the next value.
What's left must be prime and so is printed to the console.
SUNIL GUTTARank #1,832 · 810 reputationPosted Jul 13, 2013, 10:04 AM EDT
Well can you say this above mentioned snippet is normalized to the codes we done(i.e mentioned in this post) till now .. just a clarification ... can it be still optimized ??
Ty for your help
Vulpes my go to man any-time
VulpesRank #55 · 29.2K reputationPosted Jul 13, 2013, 10:18 AM EDT
You could also express it like this and get rid of the continue: