# Write A C# Program To Check Prime Number

Do you want to know how to check prime number in C#? In this C# tutorial, I will show you different ways to write a C# program to check prime number.

To check if a number is a prime in C#, you can use various methods. The basic method involves iterating from 2 to the square root of the number, checking for any divisors. Alternatively, the Sieve of Eratosthenes efficiently finds all primes up to a certain limit. For large numbers, probabilistic methods like Fermat’s Little Theorem can be used, though they are not always conclusive.

## C# Program To Check Prime Number

Now, let us see various ways to find prime number in c#. Here are the 4 different methods to determine a prime number in c#.

### 1. Find prime number in C# using Basic method

The most straightforward approach to check if a number is prime in C# is to try dividing it by all numbers less than itself and greater than one. If the number is divisible by any of these, it’s not prime.

Here is the complete code to check prime number in C#:

``````using System;

class Program
{
static void Main()
{
int number = 29;
bool isPrime = IsPrime(number);
Console.WriteLine(\$"{number} is prime: {isPrime}");
}

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;
}
}
``````

Once you execute the code using a Windows application, it will show the output like in the below screenshot.

### 2. Determine a prime number in c# using sieve of eratosthenes

For checking prime numbers in C# on a larger scale, the Sieve of Eratosthenes is an efficient algorithm. It works by iteratively marking the multiples of each prime number starting from 2. Here is the complete code to find prime numbers using c#.

``````using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
int limit = 100;
IEnumerable<int> primes = SieveOfEratosthenes(limit);

foreach (int prime in primes)
{
Console.WriteLine(prime);
}
}

static IEnumerable<int> SieveOfEratosthenes(int limit)
{
bool[] isPrime = new bool[limit + 1];
for (int i = 2; i <= limit; i++) isPrime[i] = true;

for (int i = 2; i <= limit; i++)
{
if (isPrime[i])
{
for (int j = i * 2; j <= limit; j += i)
isPrime[j] = false;

yield return i;
}
}
}
}
``````

### 3. Find prime number using lambda expression in c#

In C#, you can utilize lambda expressions to find prime numbers in an elegant and concise way. Lambda expressions are a core feature of C# that enable you to write anonymous methods with a clean and succinct syntax. When it comes to finding prime numbers, you can combine lambda expressions with LINQ (Language Integrated Query) to create an efficient and readable solution.

Here’s how you can write a method to find prime numbers using lambda expressions in C#:

``````using System;
using System.Linq;

class Program
{
static void Main()
{
int number = 29;
bool isPrime = IsPrime(number);
Console.WriteLine(\$"{number} is prime: {isPrime}");
}

static bool IsPrime(int number)
{
return number > 1 && Enumerable.Range(2, number - 2).All(x => number % x != 0);
}
}

``````

In this example:

• `Enumerable.Range(2, number - 2)` generates a sequence of integers from 2 to `number - 1`.
• The `.All()` method is a LINQ extension method that checks whether all elements in the sequence satisfy the condition specified by the lambda expression.
• The lambda expression `x => number % x != 0` specifies the condition for an integer `x` to not be a divisor of `number`.

Here, you can see the output in the screenshot below:

### 4. Calculate prime numbers in C# Fermat’s Little Theorem

Now let us see how to calculate prime numbers in C# fermat’s little theorem.

``````using System;
using System.Numerics;

class Program
{
static void Main()
{
int number = 29;
bool isPrime = IsPrimeFermat(number);
Console.WriteLine(\$"{number} is prime: {isPrime}");
}

static bool IsPrimeFermat(int number)
{
if (number <= 1) return false;
if (number == 2) return true;

Random rand = new Random();
for (int i = 0; i < 5; i++)
{
int a = rand.Next(2, number);
if (BigInteger.ModPow(a, number - 1, number) != 1)
return false;
}

return true;
}
}
``````

## How to find nth prime number in c#

Now, let us see, how to find nth prime number in c#.

You can use this function in a loop to find the nth prime number. This method starts from the first prime number (2) and checks each subsequent number until it finds the nth prime.

``````using System;

class Program
{
static void Main()
{
int nthPrime = FindNthPrime(10); // Change this to find the nth prime number
Console.WriteLine(\$"{nthPrime} is the 10th prime number.");
}

static int FindNthPrime(int n)
{
int count = 0;
int number = 2;

while (true)
{
if (IsPrime(number))
{
count++;
if (count == n)
return number;
}
number++;
}
}

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.Sqrt(number);
for (int i = 3; i <= boundary; i += 2)
{
if (number % i == 0)
return false;
}
return true;
}
}
``````

This is how to find nth prime number in C#.

## Conclusion

While the basic method of checking for prime numbers is sufficient for most applications, other methods like the Sieve of Eratosthenes, LINQ, and Fermat’s Little Theorem offer alternatives that can be more efficient, especially when dealing with larger numbers. In this C# tutorial, I have explained, how to write a c# program to check prime number.

You may also like: