C# program to count prime numbers in an array

Prime numbers are natural numbers greater than 1 with exactly two factors: 1 and themselves. Examples include 2, 3, 5, 7, 11, 13, and so on. The smallest prime number is 2, which is also the only even prime number.

Problem Description

We are given an array and need to find the count of prime numbers present in that array using C#. Below are examples to understand the problem

Examples

Input: array = {3, 6, 12, 7, 9, 11}

Output: Count = 3 (prime numbers: 3, 7, 11)

Explanation: The numbers 3, 7, and 11 have only two factors: 1 and themselves, so they are prime numbers.

Input: array = {4, 6, 19, 23, 33}

Output: Count = 2 (prime numbers: 19, 23)

Explanation: The numbers 19 and 23 are prime numbers as they have only two factors: 1 and themselves.

Prime Number Identification 3 Prime ? 1, 3 6 Not Prime ? 1, 2, 3, 6 7 Prime ? 1, 7 11 Prime ? 1, 11 Array: {3, 6, 7, 11} ? Prime Count: 3 Only numbers with exactly 2 factors are prime

Using Division Method

The division method checks if a number has any divisors other than 1 and itself. We only need to check divisors up to ?n because factors repeat beyond this point. Numbers less than or equal to 1 are not considered prime.

Algorithm Steps

  • Create a function to check if a number is prime.

  • Loop from 2 to ?number and check for divisors.

  • If any divisor is found, the number is not prime.

  • Count all prime numbers found in the array.

Example

using System;

class PrimeCounter {
    static bool IsPrime(int num) {
        if (num <= 1) return false;
        if (num == 2) return true;
        if (num % 2 == 0) return false;
        
        for (int i = 3; i * i <= num; i += 2) {
            if (num % i == 0) return false;
        }
        return true;
    }
    
    static int CountPrimes(int[] arr) {
        int count = 0;
        foreach (int num in arr) {
            if (IsPrime(num)) {
                count++;
                Console.WriteLine(num + " is prime");
            }
        }
        return count;
    }
    
    static void Main() {
        int[] array = {3, 6, 12, 7, 9, 11};
        Console.WriteLine("Array elements: {" + string.Join(", ", array) + "}");
        int primeCount = CountPrimes(array);
        Console.WriteLine("Total prime numbers: " + primeCount);
    }
}

The output of the above code is

Array elements: {3, 6, 12, 7, 9, 11}
3 is prime
7 is prime
11 is prime
Total prime numbers: 3

Time Complexity: O(n × ?m), where n is array size and m is the largest number.

Space Complexity: O(1) constant space.

Using Sieve of Eratosthenes

The Sieve of Eratosthenes efficiently finds all prime numbers up to a given limit by marking multiples of each prime as composite. This approach is more efficient when dealing with multiple prime checks or larger numbers.

Algorithm Steps

  • Create a boolean array of size (maximum array element + 1).

  • Mark 0 and 1 as non-prime.

  • For each prime starting from 2, mark its multiples as non-prime.

  • Count array elements that are marked as prime.

Example

using System;
using System.Linq;

class SievePrimeCounter {
    static int CountPrimesUsingSieve(int[] arr) {
        if (arr.Length == 0) return 0;
        
        int max = arr.Max();
        if (max < 2) return 0;
        
        // Create sieve array
        bool[] isPrime = new bool[max + 1];
        Array.Fill(isPrime, true);
        isPrime[0] = isPrime[1] = false;
        
        // Mark non-primes using sieve
        for (int i = 2; i * i <= max; i++) {
            if (isPrime[i]) {
                for (int j = i * i; j <= max; j += i) {
                    isPrime[j] = false;
                }
            }
        }
        
        // Count primes in the array
        int count = 0;
        foreach (int num in arr) {
            if (num > 1 && isPrime[num]) {
                count++;
                Console.WriteLine(num + " is prime");
            }
        }
        
        return count;
    }
    
    static void Main() {
        int[] array = {4, 6, 19, 23, 33, 2};
        Console.WriteLine("Array elements: {" + string.Join(", ", array) + "}");
        int primeCount = CountPrimesUsingSieve(array);
        Console.WriteLine("Total prime numbers: " + primeCount);
    }
}

The output of the above code is

Array elements: {4, 6, 19, 23, 33, 2}
19 is prime
23 is prime
2 is prime
Total prime numbers: 3

Time Complexity: O(m × log(log(m)) + n), where m is the maximum number and n is array size.

Space Complexity: O(m) for the sieve array.

Comparison of Methods

Method Time Complexity Space Complexity Best Use Case
Division Method O(n × ?m) O(1) Small arrays or limited memory
Sieve of Eratosthenes O(m × log(log(m)) + n) O(m) Large arrays with high numbers

Conclusion

Counting prime numbers in an array can be efficiently accomplished using either the division method for smaller datasets or the Sieve of Eratosthenes for larger arrays. The division method offers constant space complexity, while the sieve method provides better time complexity for multiple prime checks within a range.

Updated on: 2026-03-17T07:04:36+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements