``````int number;
Console.Write("Enter a number: ");

int ctr = 2;
while(ctr <= number)
{
if(number % ctr == 0)
break;
ctr++;
}
if(ctr == number)
Console.WriteLine("Prime");
else
Console.WriteLine("Not Prime");
``````

Edited by happygeek: fixed formatting

``````int number;
Console.Write("Enter a number: ");

int ctr = 2;
while(ctr <= number)
{
if(number % ctr == 0)
break;
ctr++;
}
if(ctr == number)
Console.WriteLine("Prime");
else
Console.WriteLine("Not Prime");

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by ddanbe

The short explaintion of a more efficient version:

When finding prime numbers, you should only test 2 and 3 to the square root of the number in increments of 2 (eg odd numbers), even if you arent going to use a list of generated prime numbers to reduce the time further.

``````int number;
Console.Write("Enter a number: ");

int ctr = 2;
int sq = (int)Math.Sqrt(number);
while(ctr < sq)
{
if(number % ctr == 0)
break;
if (ctr==2) { ctr++; } else {ctr+=2;}
}
if(ctr == sq)
Console.WriteLine("Prime");
else
Console.WriteLine("Not Prime as divides by "+ctr.ToString());