Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graphoarty/2c74b6605874629d9f6fd0b186ea431c to your computer and use it in GitHub Desktop.
Save graphoarty/2c74b6605874629d9f6fd0b186ea431c to your computer and use it in GitHub Desktop.
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