1
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number: ");
        int a = sc.nextInt();
        for ( int i = 1; i < a; i++) {
            int []div = {a/i};
            if (a%i == 0 ) System.out.print(Arrays.toString(div) + " ");
        }
}

Input: 68 Output : [68] [34] [17] [4] [2]

Question is how to make a single array so it looks like [68, 34, 17, 4, 2] ?

1 Answer 1

5

Since you can't know how many elements the div has in advance (it requires factorization of a), you have several options:

1. Calculate the size of div as a separate step:

int a = 68;
int n = 0; // length of the result
for (int i = 1; i < a; i++) {
    if (a % i == 0) {
        n++;
    }
}
int[] div = new int[n];
n = 0;

for (int i = 1; i < a; i++) {
    if (a % i == 0) {
        div[n++] = a / i;
    }
}
System.out.println(Arrays.toString(div));

2. Use mutable growing collection instead of the array:

int a = 68;
List<Integer> div = new ArrayList<>();
for (int i = 1; i < a; i++) {
    if (a % i == 0) {
        div.add(a / i);
    }
}
System.out.println(div);

If you need, you can convert the collection to the primitive array afterwards:

int[] divArr = div.stream().mapToInt(Integer::intValue).toArray();
System.out.println(Arrays.toString(divArr));

3. One-liner using Stream:

As @Eritrean suggested in the comments, you can use IntStream to build your array:

int a = 68;
int[] div = IntStream
    .range(1, a)
    .filter(i -> a % i == 0)
    .map(i -> a / i)
    .toArray();
System.out.println(Arrays.toString(div));

It will be more efficient than the second variant, as it avoids creating Integer wrappers.


P.S. I omitted main method and Scanner initialization for brevity. You can put it back, if you need, for example:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number: ");
    int a = sc.nextInt();
    int n = 0; // length of the result
    for (int i = 1; i < a; i++) {
        if (a % i == 0) {
            n++;
        }
    }
    int[] div = new int[n];
    n = 0;

    for (int i = 1; i < a; i++) {
        if (a % i == 0) {
            div[n++] = a / i;
        }
    }
    System.out.println(Arrays.toString(div));
}
Sign up to request clarification or add additional context in comments.

5 Comments

Instead of storing in list and then converting to array, one could just use an IntStream instead: int[] div = IntStream.range(1, a).filter(i -> a % i == 0).map(i -> a / i).toArray();
@Eritrean, good point, thank you.
Thank you for the answers but input must be taken from Scanner method. And program to print an array of dividers. So in example I enter number 44 and the program prints [44, 22, 11, 4, 2]
@Proximo_224, I omitted the Scanner creation and int a = sc.nextInt(); for brevity. You can put it back if you need. Otherwise the approaches are generic and applicable for any value a.
Thank you sir it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.