Answers for "get prime number c#"

C#
1

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
0

find prime number in c #

static Boolean PrimerNumber (int x) //The function return a value of true or false for a prime number
        {
            int i, count=0;
            bool flag=false;
            for (i = 2; i < x; i++)
            {
                if (x % i == 0)
                {
                    cont = 1;
                    break;
                }
            }
            if (count == 0)
                flag = true;
            return flag;
        }
Posted by: Guest on May-22-2021
0

is prime number c#

using System;
namespace Demo {
   class MyApplication {
      public static void Main() {
         int n = 5, a = 0;
         for (int i = 1; i <= n; i++) {
            if (n % i == 0) {
               a++;
            }
         }
         if (a == 2) {
            Console.WriteLine("{0} is a Prime Number", n);
         } else {
            Console.WriteLine("Not a Prime Number");
         }
         Console.ReadLine();
      }
   }
}
Posted by: Guest on August-03-2021
-1

how to check prime number in c#

using System;  
  public class PrimeNumberExample  
   {  
     public static void Main(string[] args)  
      {  
          int n, i, m=0, flag=0;    
          Console.Write("Enter the Number to check Prime: ");    
          n = int.Parse(Console.ReadLine());  
          m=n/2;    
          for(i = 2; i <= m; i++)    
          {    
           if(n % i == 0)    
            {    
             Console.Write("Number is not Prime.");    
             flag=1;    
             break;    
            }    
          }    
          if (flag==0)    
           Console.Write("Number is Prime.");       
     }  
   }
Posted by: Guest on May-03-2021

C# Answers by Framework

Browse Popular Code Answers by Language