Wednesday, March 9, 2016

Check if the given number is Prime Number or not

Code:

using System;

public class Test
{
    public static void Main()
    {
    int n = 25;
        
        Console.WriteLine("Input: " + n);
        
        if(new Test().IsPrime(n) == true)
        {
        Console.WriteLine("Output: " + n + " is Prime Number");
        }
        else
        {
        Console.WriteLine("Output: " + n + " is not Prime Number");
        }

        Console.ReadKey();
    }

    public bool IsPrime(int n)
{
for(int i=2; i * i < n; i++)
{
if(n % i == 0)
{
return false;
}
}

return true;
}

}

Output:

Input: 25
Output: 25 is Prime Number

Run Time: O(N^(1/2))

No comments:

Post a Comment