'How do i find if a positive integer is prime or not? [closed]
Am very new to the C# language and i have been working on this program to find a positive integer is a prime or not. what am i doing wrong and how do i accomplish that. thank you in advance
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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
