Find All Prime Numbers in Array
C++
Medium
6 views
Problem Description
Identify and display all prime numbers present in given array. This combines prime checking logic with array traversal.
Logic: For each number, check if it's prime using division method
Official Solution
void question10_prime_in_array() {
int arr[] = {10, 13, 15, 17, 20, 23};
int size = 6;
cout << "Prime numbers: ";
for(int i = 0; i < size; i++) {
bool isPrime = true;
if(arr[i] < 2) isPrime = false;
for(int j = 2; j * j <= arr[i]; j++) {
if(arr[i] % j == 0) {
isPrime = false;
break;
}
}
if(isPrime) {
cout << arr[i] << " ";
}
}
cout << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!