0

write a program that read an integer from user and print all of its divisors in java

Please write this program.

27th Oct 2018, 12:50 PM
Zain Ali
Zain Ali - avatar
1 ответ
+ 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)); } }
27th Oct 2018, 4:29 PM
Seniru
Seniru - avatar