Created
March 3, 2019 03:52
-
-
Save graphoarty/2c74b6605874629d9f6fd0b186ea431c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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