+ 1

# How to write the c++ code of the following question!?

The first 11 prime integers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, and 31. A positive integer between 1 and 1000 (inclusive), other than the first 11 prime integers, is prime if it is not divisible by 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, and 31. Write a C++ program that prompts the user to enter a positive integer between 1 and 1000 (inclusive) and then outputs whether the number is prime. If the number is not prime, then output all the numbers, from the list of the first 11 prime integers, which divide the number.

1 Answer

- 1

int n;
cin >> n;
vector<int> prime {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
bool is = false;
for (int i : prime) if (n % i == 0) {
cout << i << ' ';
is = true;
}
if (is) cout << n << " is not prime";
else cout << n << " is prime";