If you are working in Java and searching for how to find factors of a number in Java, then you are in the right place. When you are working with numbers in programming, you may frequently need to perform operations like finding factors. Factors of a number are integers that divide it completely without leaving a remainder.
In this article, we will explore various methods to find all factors of a given number in Java with example code.
What Are Factors?
Factors of a number are integers that evenly divide the number. For example, the factors of 12 are 1, 2, 3, 4, 6, and 12. These numbers divide 12 without leaving a remainder. Will understand this in code example.
Also Read:
Factors of a Number Using Loops
Using a for
Loop
First, we will use for loop to find factors of a number in Java. Here is a simple Java program Example:
import java.util.Scanner;
public class FactorFinder {
public static void main(String[] args) {
// Create a Scanner object to take user input
Scanner scanner = new Scanner(System.in);
// Ask the user for a number
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println("Factors of " + number + " are:");
// Loop through potential factors
for (int i = 1; i <= number; i++) {
if (number % i == 0) {
// If divisible, print the factor
System.out.print(i + " ");
}
}
scanner.close();
}
}
Using a while
Loop
The second approach to finding the factors of a number is a while loop. Here is a simple Java program Example:
import java.util.Scanner;
public class FactorUsingWhile {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println("Factors of " + number + " are:");
int i = 1;
while (i <= number) {
if (number % i == 0) {
System.out.print(i + " ");
}
i++;
}
scanner.close();
}
}
Using a do-while
Loop
import java.util.Scanner;
public class FactorUsingDoWhile {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println("Factors of " + number + " are:");
int i = 1;
do {
if (number % i == 0) {
System.out.print(i + " ");
}
i++;
} while (i <= number);
scanner.close();
}
}
Optimized Approach to Find Factors of a Number in Java
Those above programs can be optimized with many fewer iterations. Instead of going from 1 up to the number, loop as far as the square root and take factors in pairs as below.
import java.util.Scanner;
public class OptimizedFactorFinder {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println("Factors of " + number + " are:");
for (int i = 1; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
System.out.print(i + " ");
if (i != number / i) {
System.out.print((number / i) + " ");
}
}
}
scanner.close();
}
}
How It Works
- Loop Until Square Root: Iterating only up to the square root of the number reduces the number of checks.
- Find Factor Pairs: When i is a factor, number / i is also a factor. This eliminates the need to loop through all numbers.
Factors of Negative Numbers
To find the factors of negative numbers, the factors can include negative integers also. Below code example will help you to handle this scenario:
import java.util.Scanner;
public class NegativeFactors {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println("Factors of " + number + " are:");
for (int i = -Math.abs(number); i <= Math.abs(number); i++) {
if (i != 0 && number % i == 0) {
System.out.print(i + " ");
}
}
scanner.close();
}
}
Other Methods to Find Factors
Recursive Approach
Another way to find factors is by using recursion.
import java.util.Scanner;
public class RecursiveFactorFinder {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println("Factors of " + number + " are:");
findFactors(number, 1);
scanner.close();
}
public static void findFactors(int number, int divisor) {
if (divisor > Math.abs(number)) {
return;
}
if (number % divisor == 0) {
System.out.print(divisor + " ");
}
findFactors(number, divisor + 1);
}
}
Using Streams in Java 8
With Java 8 and above, you can leverage streams to find factors more concisely:
import java.util.Scanner;
import java.util.stream.IntStream;
public class StreamFactorFinder {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println("Factors of " + number + " are:");
IntStream.rangeClosed(-Math.abs(number), Math.abs(number))
.filter(i -> i != 0 && number % i == 0)
.forEach(i -> System.out.print(i + " "));
scanner.close();
}
}
How It Works
- IntStream.rangeClosed: Generates a stream of numbers from -|number| to |number|.
- Filter: Keeps only those numbers that divide the input number without a remainder.
- ForEach: Prints each factor.
Conclusion
Finding factors of a number in Java is a fundamental programming task with many practical applications. By mastering this, you strengthen your understanding of loops, conditionals, and optimization techniques. Whether you’re preparing for coding interviews or solving real-world problems, this skill will come in handy. Try implementing the examples above and experimenting with your own variations to deepen your knowledge.