package net.megaroid.math;

import java.util.Scanner;

public class Factors {
	
	private int mNumber;
	
	public Factors(int number) {
		mNumber = number;
	}

	public int getNumber() {
		return mNumber;
	}
	
	public void printAllFactors() {
		System.out.print("Factors: ");
		int sqrtN = (int)Math.sqrt(mNumber);
		for (int i = 1; i <= sqrtN; i++) {
			if (mNumber % i == 0) {
				System.out.print(i + " ");
				if (i * i != mNumber) {
					System.out.print((mNumber / i) + " ");
				}
			}
		}
		System.out.println("");
	}
	
	public static void main(String[] args) {
		
		System.out.print("Input a number : ");

		Scanner scanner = new Scanner(System.in);
		int num = scanner.nextInt();
		scanner.close();
		
		Factors f = new Factors(num);
		System.out.println("The number is " + f.getNumber());
		
		f.printAllFactors();
	}
}

@@ -95,16 +95,63 @@
 Number;%0A
+%09private int%5B%5D mFactors; %0A%09private int mCount;%0A
 %09%0A%09publi
@@ -286,77 +286,324 @@
 %7B%0A%09%09
-System.out.print(%22Factors: %22);%0A%09%09int sqrtN = (int)Math.sqrt(mNumber);
+if (mFactors == null) %7B%0A%09%09%09initialize();%0A%09%09%7D%0A%09%09%0A%09%09System.out.print(%22Factors: %22);%0A%09%09for (int i = 0; i %3C mCount; i++) %7B%0A%09%09%09System.out.print(mFactors%5Bi%5D + %22 %22);;%0A%09%09%7D%0A%09%09System.out.println(%22%22);%0A %09%7D%0A%09%0A%09private void initialize()%0A%09%7B%0A%09%09int sqrtN = (int)Math.sqrt(mNumber);%0A%09%09mCount = 0;%0A%09%09%0A%09%09mFactors = new int%5BsqrtN * 2%5D;%0A%09%09
 %0A%09%09f
@@ -671,33 +671,30 @@
 %09%09%09%09
-System.out.print(i + %22 %22)
+mFactors%5BmCount++%5D = i
 ;%0A%09%09
@@ -728,26 +728,29 @@
 %09%09%09%09
-System.out.print((
+mFactors%5BmCount++%5D = 
 mNum
@@ -760,16 +760,8 @@
  / i
-) + %22 %22)
 ;%0A%09%09
@@ -777,34 +777,8 @@
 %09%09%7D%0A
-%09%09System.out.println(%22%22);%0A
 %09%7D%0A%09

Add mFactors member which keep factors of mNumber