prime numbers c# code example
Example 1: isprime c#
public static bool IsPrime(int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i+=2)
if (number % i == 0)
return false;
return true;
}
Example 2: 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.");
}
}