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] ?

Aivean
  • 10,692
  • 25
  • 39

1 Answers1

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));
}
Aivean
  • 10,692
  • 25
  • 39
  • 2
    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 Nov 15 '21 at 19:54
  • @Eritrean, good point, thank you. – Aivean Nov 15 '21 at 19:57
  • 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 Nov 15 '21 at 20:12
  • 1
    @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`. – Aivean Nov 15 '21 at 20:14
  • 1
    Thank you sir it works. – Proximo_224 Nov 15 '21 at 20:26