import java.util.ArrayList; import java.util.Scanner; public class Main { static Scanner scan; public static void main(String[] args) { scan = new Scanner(System.in); ArrayList<Integer> factors = new ArrayList<Integer>(); System.out.println("Enter the number."); int toFindFactorOf = scan.nextInt(); int limit = toFindFactorOf; int counter = 2; while (counter < toFindFactorOf && limit >= 1) { if (limit % counter == 0) { factors.add(counter); limit = limit / counter; // wanted to put this here explicitly // for better understanding if (limit % counter == 0) { continue; } } else { counter++; } } if (factors.isEmpty()) { if (toFindFactorOf == 0) { System.out.println(toFindFactorOf + " is neither prime nor composite!"); } else { System.out.println(toFindFactorOf + " is a prime number!"); } } else { System.out.println(factors); } } }