Ask Question
19 September, 22:06

Write a function, factors, that takes an integer n, and returns a list of values that are the positive divisors of n. Note: 0 is not a divisor of any integer, 1 divides every number, and n divides itself.

+5
Answers (1)
  1. 19 September, 22:08
    0
    public int[] factors (int n)

    {

    int arr[]=new int[n];

    int count=0;

    for (int i=1; i<=n; i++)

    if (n%i==0)

    {

    arr[count]=i;

    count++;

    }

    return arr;

    }

    Explanation:

    Factors are the numbers you multiply to get another number. For instance, factors of 15 are 3 and 5, because 3*5 = 15.

    In the program, we have an array of length N to store the factors.

    Since we don't know the number of factors of N, so we have taken size of upper bound N to store list of factors in the array arr.

    Within the loop the number gets divide by Integers from 1 to that number until result is 0. If it is 0, that number is stored in array.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a function, factors, that takes an integer n, and returns a list of values that are the positive divisors of n. Note: 0 is not a ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers