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 % i == 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.

4
2
HooWoo 90 points

                                    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 &lt; x; i++)
            {
                if (x % i == 0)
                {
                    cont = 1;
                    break;
                }
            }
            if (count == 0)
                flag = true;
            return flag;
        }

4 (2 Votes)
0
4.5
8

                                    using System;  
  public class PrimeNumberExample  
   {  
     public static void Main(string[] args)  
      {  
          int n, i, m=0, flag=0;    
          Console.Write(&quot;Enter the Number to check Prime: &quot;);    
          n = int.Parse(Console.ReadLine());  
          m=n/2;    
          for(i = 2; i &lt;= m; i++)    
          {    
           if(n % i == 0)    
            {    
             Console.Write(&quot;Number is not Prime.&quot;);    
             flag=1;    
             break;    
            }    
          }    
          if (flag==0)    
           Console.Write(&quot;Number is Prime.&quot;);       
     }  
   }  

4.5 (8 Votes)
0
0
1
T.Baugh 105 points

                                    using System;
namespace Demo {
&nbsp; &nbsp;class MyApplication {
&nbsp; &nbsp; &nbsp; public static void Main() {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int n = 5, a = 0;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int i = 1; i &lt;= n; i++) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (n % i == 0) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a++;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (a == 2) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;{0} is a Prime Number&quot;, n);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Not a Prime Number&quot;);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.ReadLine();
&nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp;}
}

0
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source