CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main() {
int foo[5] = {49, 8, 5, 202, 13}; // array declaration.
for (int i=0; i<5; i++) // access all elements of array
{
bool prime=true; // use a boolean. It will be set to false if number inside array is not prime
for (int j=2; j*j<=foo[i]; j++) // accessing all numbers <= of number in the array
{
if (foo[i] % j == 0) // check if the number inside array can be divided by any j.
{
prime=false; // if it is divisble for any value of j then it is not prime, so set boolean to false.
break; // stop the loop
}
}
if(prime) cout << foo[i] << " "; // if boolean was not changed to false and it is still true, the number is prime and we print it!
}
return 0;
}
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run