print all divisors of a number code example
Example 1: all devisor of a number java
public static ArrayList<Integer> getDivisors(int n){
ArrayList<Integer> divisors = new ArrayList<>();
for (int i = 2; i * i <= n; ++i)
if (n % i == 0) {
divisors.add(i);
if (i != n / i) divisors.add(n / i);
}
return divisors;
}
Example 2: c++ find number of divisors
int countDivisors(int n) {
int cnt = 0;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
cnt++;
else
cnt = cnt + 2;
}
}
return cnt;
}