Question - Write a C# program to find if a positive integer is prime or not?
Answer -
static void Main(string[] args)
{
if (FindPrime(47))
{
Console.WriteLine("Prime");
}
else
{
Console.WriteLine("Not Prime");
}
Console.ReadLine();
}
internal static bool FindPrime(int number)
{
if (number == 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var squareRoot = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= squareRoot; i += 2)
{
if (number % i == 0) return false;
}
return true;
}