Check if 29 is prime and print Prime or Not Prime.
Ready — edit the code above and click Run.
using System;
class Program
{
static void Main()
{
int n = 29;
bool isPrime = n > 1;
for (int i = 2; i * i <= n && isPrime; i++)
if (n % i == 0) isPrime = false;
Console.WriteLine(isPrime ? "Prime" : "Not Prime");
}
}
Try solving on your own first, then reveal the official answer.
Loop only to sqrt(n) (here i*i <= n)—efficient prime test for interviews.