---
title: "Prime Number Checker"  
description: "Prime Number Checker"  
author: "Steilla Mitchel"  
published: 2024-06-11  
updated: 2024-06-13  
canonical: https://www.mindstick.com/forum/160722/prime-number-checker  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 2 minutes  

---

# Prime Number Checker

Develop a C# [function to check](https://www.mindstick.com/forum/161900/write-a-function-to-check-if-a-number-is-prime-with-explanation) whether a given number n is a prime number. A prime number is a [natural](https://yourviews.mindstick.com/story/4439/5-natural-herbs-to-cure-migraine) number greater than 1 that has no positive divisors other than 1 and itself.

## Replies

### Reply by Ashutosh Patel

#### Prime Number Checker in C#

Below is a simple implementation of a prime number checker in C#,

## Example-

```cs
using System;
class Program
{
   static void Main()
   {
       // Prompt the user to enter a number
       Console.WriteLine("Enter a number to check Prime or not ");
       int number = int.Parse(Console.ReadLine());
       // Check if the entered number is prime or not
       if (IsPrime(number))
       {
           Console.WriteLine(number + " is a prime number.");
       }
       else
       {
           Console.WriteLine(number + " is not a prime number.");
       }
       Console.ReadLine();
   }
   // Function to check if a number is prime
   static bool IsPrime(int number)
   {
       // 0 and 1 are not prime numbers
       if (number < 2)
           return false;
       // Check for divisibility by numbers up to the square root of the number
       for (int i = 2; i <= Math.Sqrt(number); i++)
       {
           if (number % i == 0)
               return false; // If the number is divisible by any number other than 1 and itself, it's not prime
       }
       return true; // If the loop completes without finding any divisors, the number is prime
   }
}
```

## In the above example-

- We ask the user to enter a `number`.
- We call the `IsPrime`[function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to check if the input number is **prime**.
- The `IsPrime`function checks if the number is less than **2** and returns **false** if it is, because **0** and **1** are not prime numbers.
- It then checks whether the number is divisible by any of these numbers by iterating from **2** to the **square root** of the number. If so, the function returns **false**, indicating that the number is **not prime.**
- If the loop completes without finding any **divisor**, the function returns **true**, which means the number is **prime**.

## Output- 1

```plaintext
Enter a number to check Prime or not
20
20 is not a prime number.
```

## Output- 2

```plaintext
Enter a number to check Prime or not
23
23 is a prime number.
```

This function must carefully [check whether](https://www.mindstick.com/forum/157543/write-a-java-program-to-check-whether-a-string-is-a-palindrome) the given quantity is **prime** or not.

**Also, Read:** [Fibonacci Sequence](https://www.mindstick.com/forum/160721/fibonacci-sequence)


---

Original Source: https://www.mindstick.com/forum/160722/prime-number-checker

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
