Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.