Răspuns:
#include <iostream>
using namespace std;
int numberOfDivisors(int n) {
int count = 0;
int middle = n / 2;
for (int i = 2;i <= middle; i++) {
if (n % i == 0) {
count++;
}
}
return count;
}
int main() {
int n;
int k;
cout << "n=";
cin >> n;
cout << "k=";
cin >> k;
cout << "Numerele naturale pana la " << n << " care au exact " << k << " diviziori:\n";
for (int i = 2;i <= n; i++) {
if (numberOfDivisors(i) == k) {
cout << i << " ";
}
}
return 0;
}
Explicație: