0
write a program that read an integer from user and print all of its divisors in java
Please write this program.
1 Antwort
+ 1
this will help
import java.util.*;
public class Program {
static ArrayList<Integer> factors(int n) {
//the factors of a number lies from 1 to sqrt of it.
var arr = new ArrayList<Integer>();
for(var i=1;i<=Math.sqrt(n);i++) {
if(n%i==0) {
//checking if n is divisible vy i and add it to array if yes.
arr.add(i);
}
}
return arr;
}
public static void main(String[] args) {
System.out.println(factors(120));
}
}