This post contains a total of 7+ Hand-Picked C Sharp Prime number checker Program Examples with Source Code. All these prime number checker programs are made using C Sharp Programming Language.
You can use the source code of these examples with credits to the original owner.
Related Posts
C Sharp Prime Number Checker Programs
1. By Zarthah
Made by Zarthah. C# Program to Check if numbers in a given range are prime or not, the program by default checks the numbers in range 10 – 100. Source
If its 'true' it means its a prime number -10 = False -9 = False -8 = False -7 = False -6 = False -5 = False -4 = False -3 = False -2 = False -1 = False 0 = False 1 = False 2 = True 3 = True 4 = False 5 = True
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static bool prime(int num)
{
if(num > 1)
{
if(num == 2)
return true;
for(int i = 2; i <= num/2; i++)
{
if(num % i == 0)
return false;
}
return true;
}
return false;
}
static void Main(string[] args)
{
Console.WriteLine("If its 'true' it means its a prime number");
// For example, we will check from -10 to 100, which those numbers are the prime number.
for(int i = -10; i <= 100; i++)
{
Console.Write(Convert.ToString(i));
Console.Write(" = ");
Console.WriteLine(prime(i));
}
}
}
}
2. By Canny
Made by Canny. A simple program to check if a Number is PRIME or NOT A PRIME NUMBER. Source
10 10 is NOT A PRIME NUMBER
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
double num, divisor = 1, counter=0;
num = Convert.ToInt32(Console.ReadLine());
do
{
if(num % divisor == 0)
{
counter++; //counts the factors
divisor++;
}
else
divisor++;
}
while(divisor<=num);
if(counter==2) //TRUE if the number has two factors
Console.Write($"{num} is a PRIME NUMBER");
else
Console.Write($"{num} is NOT A PRIME NUMBER");
}
}
}
3. By Samuel Kurnas
Made by Samuel Kurnas. Source
7 7 is prime number
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
//insert some number
int number = Convert.ToInt32(Console.ReadLine());
if (isPrimeNumber(number))
Console.WriteLine(number + " is prime number");
else
Console.WriteLine(number + " is not prime number");
}
public static bool isPrimeNumber(int number)
{
if (number == 1)
return false;
if (number == 2)
return true;
int loops = Convert.ToInt32(Math.Floor(Math.Sqrt(number)));
for (int i = 2;i<=loops;i++)
{
//modulo
if (number%i == 0)
return false;
}
return true;
}
}
}
4. By ermain F
Made by ermain F. Prime number checker program that takes an arbitrary integer and determines whether it is prime. Source
The number you entered is 12 12 is not a prime number
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Integers
{
class PrimeNumbers
{
static bool isAPrime( int numba)
{
int factor =0;
for (int loop=1; loop<=numba; loop++){
if ( numba % loop == 0)
factor++;
}
if (factor == 2)
return true;
return false;
}
static void Main(string[] args)
{
Console.WriteLine("Prime Number Checker version 1.0.0\nCode By Germain.F..................\n\n");
Int32 number=Convert.ToInt32( Console.ReadLine());
Console.WriteLine ("The number you entered is {0}", number);
if ( isAPrime(number) )
Console.WriteLine ("{0} is a prime number", number);
else
Console.WriteLine ("{0} is not a prime number", number);
Console.WriteLine("\n.......................Please don't forget to leave a like or a comment.\nThanks for testing!");
}
}
}
5. By Mikeul
Made by Mikeul. Source
9 9 is not a prime number
using System;
namespace Prime
{
class Program
{
static void Main(string[] args)
{
int input = int.Parse(Console.ReadLine());
if(input.IsPrime())
Console.WriteLine($"{input} is a prime number");
else
Console.WriteLine($"{input} is not a prime number");
}
}
public static class Count
{
public static bool IsPrime(this int n)
{
if(n < 2)
return false;
for(int i = 2; i <= n/2; i++)
if(n % i == 0)
return false;
return true;
}
}
}
6. By Domagoj Puljiฤ
Made by Domagoj Puljiฤ. Source
3 Number is prime.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int n=Convert.ToInt32(Console.ReadLine());
if(isPrime(n)){
Console.WriteLine("Number is prime.");
}
else {
Console.WriteLine("Number is not prime.");
}
}
static bool isPrime(int number)
{
if(number==1 || number==0){
return false;
}
for(int i=2;i<=Math.Sqrt(number);i++){
if(number%i==0){
return false;
}
}
return true;
}
}
}
7. By Damon Keane
Made by Damon Keane. A simple C Sharp program to see if the input number is prime or not. Source
15 Not Prime
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
public static void Prime(int n) {
bool prime = true;
int x = Convert.ToInt32(Math.Sqrt(n));
if(n == 1)
{
prime = false;
}
for(int i = 2; i <= x; i++)
{
if(n%i == 0)
{
prime = false;
}
}
if(prime)
{
Console.WriteLine("Is Prime");
}
else {
Console.WriteLine("Not Prime");
}
}
static void Main(string[] args)
{
int num = Convert.ToInt32(Console.ReadLine());
Prime(num);
}
}
}
8. By Divi
Made by Divi. Enter any integer ( not more than 32bit) and the program will tell if the number is prime or not. Source
19 19 is a prime number.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main()
{
int x;
x = Convert.ToInt32(Console.ReadLine());
int result = PrimeCheck(x);
if (result==0)
{Console.WriteLine("{0} is not a prime number.",x);}
else {Console.WriteLine("{0} is a prime number.",x);};}
static int PrimeCheck(int x)
{
for(int y=2;y<x;y++)
{if(x%y==0)
{return 0;}}
return 1;}}}