Answers for "c# program to find prime number or not"

C#
2

get prime number c#

// Assuming that the number passed is higher than 1.


// This function will check if the number is prime by using all the previous
// numbers. There's no need to check further than the square root 
// Returns true or false.
public static bool isPrime(int number)
        {
            double sqrNum = Math.Sqrt(number);
            for (int i = 2; i <= sqrNum; i++) if (number % 2 == 0) return false;
            return true;
        }


// This function is essentially the same
// but will only compare with numbers from a prime number list
public static bool isPrime(int number, int[] primeList) 
       {
            double sqrNum = Math.Sqrt(number);

            foreach(int prime in primeList) { 
                if (prime > sqrNum) return true;
                else if (number % prime==0) return false;
            }
            return true;
       }
// You can expand your primeList using any of the functions, 
//to better use the second function.
Posted by: Guest on August-06-2021
-1

how to check if a number is prime or not c#

if (intNumber % 2 == 0)
{
	// this number is prime
}
else
{
	// this number is not prime 
}
Posted by: Guest on May-06-2021

Code answers related to "c# program to find prime number or not"

C# Answers by Framework

Browse Popular Code Answers by Language