I am trying to get the factor of a number and find the minimum distance between the factor of that number. I was trying to accomplish this process in 2 steps first finding the factors and then taking those numbers to find the minimum distance between them. I used this to find the factorial of a number
Scanner input = new Scanner(System.in);
int n;
System.out.println("Enter a number");
n = input.nextInt();
if(n <= 0){
System.out.println("cant input a number less than or equal to zero");
input.close();
return;
}
System.out.println("factors of " + "" + n+ "" + " are" );
for(int i = 1; i <= n; i++){
if(n % i == 0){
System.out.println(i);
}
}
How could I get those output again for finding minimum distance between them I tried this logic
int[] a = new int[] {i};
Arrays.sort(a);
int minDiff = a[1]-a[0];
for (int i = 2 ; i != a.length ; i++) {
minDiff = Math.min(minDiff, a[i]-a[i-1]);
}
System.out.println(minDiff);
My problem is, I don't know how to store those output in an array for further calculation.