Java Program – Find All Factors of a Number

A factor is a number that does not leave a reminder when it divides the given number. In this tutorial, we shall write Java programs that print all factors of a given number.

We shall discuss two ways. Firstly the easy one, where we shall just iterate over the range of numbers from one to the given number and check each number if it is a factor. Then, we shall improve on the number of iterations, by taking into some of the important facts about factors of a number.

Example 1 – Find All Factors of a Number

In the following Java program, we shall find all the factors of a given number. We shall take the number in a variable num.

Write a for loop, that checks each number from 1 to that number, whether this number is a factor.

To check if the reminder is zero or not, we shall use modulus operator.

Example.java

</>
Copy
/**
 * Java Program - Print Factors of Number
 */

public class Example {

	public static void main(String[] args) {
		//number
		int num = 8;
		
		//find all factors
		for(int i = 1; i <= num; ++i) {
			//check if i is a factor of num
			if(num % i == 0) {
				System.out.print(i+" ");
			}
		}
	}
}

Run the above Java Program. It prints all the factors of number 8.

Output

1 2 4 8

For Loop Execution

The following snippet provides values of different variables in the program, during each iteration of for loop in the above Java program.

Iteration 1
num : 8
i   : 1
condition (8 % 1 == 0) is true
1 is a factor.

Iteration 2
num : 8
i   : 2
condition (8 % 2 == 0) is true
2 is a factor.

Iteration 3
num : 8
i   : 3
condition (8 % 3 == 0) is false
3 is not a factor.

Iteration 4
num : 8
i   : 4
condition (8 % 4 == 0) is true
4 is a factor.

Iteration 5
num : 8
i   : 5
condition (8 % 5 == 0) is false
5 is not a factor.

Iteration 6
num : 8
i   : 6
condition (8 % 6 == 0) is false
6 is not a factor.

Iteration 7
num : 8
i   : 7
condition (8 % 7 == 0) is false
7 is not a factor.

Iteration 8
num : 8
i   : 8
condition (8 % 8 == 0) is true
8 is a factor.

Example 2 – Efficient way to print All Factors of a Number

Following is more efficient if the number is relatively large. In the for loop condition, i <= num/i, we are dynamically reducing the upper limit. Hence, fewer loop executions. This reduction in the upper limit does not effect the factors of a number, but reduces the for loop block iterations.

Example.java

</>
Copy
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Java Program - Print Factors of Number
 */

public class Example {

	public static void main(String[] args) {
		//number
		int num = 80;
		//store factors in this list
		List<Integer> factors = new ArrayList<Integer>();
		
		for(int i = 1; i <= num/i; ++i) {
			if(num % i == 0) {
				//if i is a factor, num/i is also a factor
				factors.add(i);
				factors.add(num/i);
			}
		}
		
		//sort the factors
		Collections.sort(factors); 
		
		//print the factors
		factors.forEach(factor -> System.out.print(factor+" "));
	}
}

Run the above program. You shall get the following output printed to the console.

Output

1 2 4 5 8 10 16 20 40 80

Are you wondering why this program is more efficient that the first one. Well! In this program, for loop iterates only 8 times, unlike the first program which would iterate 80 times.

Conclusion

In this Java Tutorial, we learned how to find all the factors of a number, with the help of detailed Java Programs.