Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Factors {
- private int start = 1, end;
- public static void main(String[] args) {
- Factors findfactors = new Factors();
- findfactors.getRange();
- findfactors.listPrimeFactorizations();
- }
- // Prompt the user for input. Save the input with the
- // appropriate variables.
- public void getRange() {
- // start = Starting number 1
- start = Prompt.getValue("Enter a starting integer from 2 to 100 -> ");
- while (start > 100 || start < 2) {
- start = Prompt.getValue("Please enter a valid starting integer from 2 to 100 -> ");
- }
- // end = Starting number 2
- end = Prompt.getValue("Enter an ending integer from " + start + " to 999 -> ");
- while (end < start || end > 999) {
- end = Prompt.getValue("Please enter a valid ending integer from " + start + " to 999 -> ");
- }
- }
- /**
- * List the integers, from start to end. Show the prime factorization for
- * each integer in the list. Call printFactorization for each integer in the
- * list.
- */
- public void listPrimeFactorizations() {
- for (; start <= end; start++) {
- // This loop checks for the amount of numbers left to factor
- printFactorization(start);
- }
- }
- // Print the prime factorization for the positive
- // integer passed in the parameter list.
- public void printFactorization(int num) {
- boolean firstTime = true; // true if 1st print
- int i = num;
- int base = 2;
- System.out.print(num + " = ");
- while (base <= num) {
- // check if divisible by the base with no remainder
- while (i % base == 0) {
- i = i / base;
- if (firstTime == false) {
- System.out.print(" * ");
- }
- System.out.print(base);
- firstTime = false;
- base = 2; // reset
- }
- base++;
- }
- System.out.println();
- }
- }
Add Comment
Please, Sign In to add comment