Answers for "how to find divisors of a number in cpp"

1

all devisor of a number java

public static ArrayList<Integer> getDivisors(int n){
        ArrayList<Integer> divisors = new ArrayList<>();
        //without 1 or n
        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;
    }
Posted by: Guest on December-09-2020
0

c++ find number of divisors

// https://www.geeksforgeeks.org/count-divisors-n-on13/
int countDivisors(int n) { 
    int cnt = 0; 
    for (int i = 1; i <= sqrt(n); i++) { 
        if (n % i == 0) { 
            // If divisors are equal, 
            // count only one 
            if (n / i == i) 
                cnt++; 
  
            else // Otherwise count both 
                cnt = cnt + 2; 
        } 
    } 
    return cnt; 
}
Posted by: Guest on July-07-2020

Code answers related to "how to find divisors of a number in cpp"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language