#include <iostream>
using namespace std;
bool is_prime(int num) {
if (num < 2) 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;
}
int main() {
int array[5];
int num;
cin >> num;
if (is_prime(num)) {
array[2] = num;
}
return 0;
}
To check that a number is prime, we just need to make sure that it is not divisible by any number from 3 to the square root of this number itself and is not a two.
And we insert it into the element with the number 2 because this is the third element in the array, since they are numbered from 0.
Comments
Leave a comment