Counting the number of primes in the array
We create an array of integers and count the number of primes using the checkPrime
function for each element of the array. If an element is prime, we increment a counter. Add the following code to the main.cpp
file:
int main(){
int arr[] = { 1, 3, 5, 4, 8, 13, 11 };
int n = 7;
int count = 0;
int isprime = 0;
// Traverse through each element of array and check if it is a prime
for(int i = 0; i < n; i++){
isprime = checkPrime(arr[i]);
if(isprime == 1){
count++;
}
}
cout << "Count of number of primes in array: " << count << endl;
return 0;
}