Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a C# program to check if a number is prime or not
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. To check if a number is prime in C#, we count how many divisors it has by iterating through all numbers from 1 to the number itself.
The basic approach uses a counter that increments each time we find a divisor. If the counter equals 2 at the end (divisible only by 1 and itself), the number is prime.
Algorithm
The algorithm for checking prime numbers follows these steps −
Initialize a counter to 0
Loop from 1 to the number (n)
If the number is divisible by the current iteration value, increment the counter
If the counter equals 2, the number is prime
Using Basic Method
This example checks if a number is prime by counting all its divisors −
using System;
class Program {
public static void Main() {
int n = 17, a = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
a++;
}
}
if (a == 2) {
Console.WriteLine("{0}: Prime Number", n);
} else {
Console.WriteLine("{0}: Not a Prime Number", n);
}
}
}
The output of the above code is −
17: Prime Number
Using Optimized Method
A more efficient approach only checks divisors up to the square root of the number, since factors come in pairs −
using System;
class Program {
public static bool IsPrime(int number) {
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
for (int i = 3; i <= Math.Sqrt(number); i += 2) {
if (number % i == 0) {
return false;
}
}
return true;
}
public static void Main() {
int[] numbers = {2, 17, 25, 29, 100};
foreach (int num in numbers) {
if (IsPrime(num)) {
Console.WriteLine("{0}: Prime Number", num);
} else {
Console.WriteLine("{0}: Not a Prime Number", num);
}
}
}
}
The output of the above code is −
2: Prime Number 17: Prime Number 25: Not a Prime Number 29: Prime Number 100: Not a Prime Number
Using Method with Range Checking
This example demonstrates checking multiple numbers in a range −
using System;
class Program {
public static void CheckPrime(int n) {
if (n <= 1) {
Console.WriteLine("{0}: Not a Prime Number (must be > 1)", n);
return;
}
int divisorCount = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
divisorCount++;
}
}
if (divisorCount == 2) {
Console.WriteLine("{0}: Prime Number", n);
} else {
Console.WriteLine("{0}: Not a Prime Number", n);
}
}
public static void Main() {
Console.WriteLine("Checking numbers from 1 to 20:");
for (int i = 1; i <= 20; i++) {
CheckPrime(i);
}
}
}
The output of the above code is −
Checking numbers from 1 to 20: 1: Not a Prime Number (must be > 1) 2: Prime Number 3: Prime Number 4: Not a Prime Number 5: Prime Number 6: Not a Prime Number 7: Prime Number 8: Not a Prime Number 9: Not a Prime Number 10: Not a Prime Number 11: Prime Number 12: Not a Prime Number 13: Prime Number 14: Not a Prime Number 15: Not a Prime Number 16: Not a Prime Number 17: Prime Number 18: Not a Prime Number 19: Prime Number 20: Not a Prime Number
Comparison of Methods
| Method | Time Complexity | Best For |
|---|---|---|
| Basic Method | O(n) | Learning, small numbers |
| Optimized Method | O(?n) | Large numbers, performance |
| Range Checking | O(n) | Multiple number validation |
Conclusion
Checking prime numbers in C# can be done using different approaches. The basic method counts all divisors, while the optimized method checks only up to the square root for better performance. Choose the method based on your specific requirements for accuracy and efficiency.
