0
0
JavaProgramBeginner · 2 min read

Java Program to Find Factors of a Number

To find factors of a number in Java, use a for loop from 1 to the number and check if the number is divisible by the loop variable using if (number % i == 0) to print the factors.
📋

Examples

Input6
OutputFactors of 6 are: 1 2 3 6
Input13
OutputFactors of 13 are: 1 13
Input1
OutputFactors of 1 are: 1
🧠

How to Think About It

To find factors of a number, think about all numbers from 1 up to that number. For each, check if it divides the number evenly with no remainder. If yes, that number is a factor.
📐

Algorithm

1
Get the input number from the user.
2
Start a loop from 1 to the input number.
3
For each number in the loop, check if it divides the input number evenly (remainder zero).
4
If yes, print that number as a factor.
5
End the loop after reaching the input number.
💻

Code

java
import java.util.Scanner;

public class Factors {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = sc.nextInt();
        System.out.print("Factors of " + number + " are: ");
        for (int i = 1; i <= number; i++) {
            if (number % i == 0) {
                System.out.print(i + " ");
            }
        }
        sc.close();
    }
}
Output
Enter a number: 6 Factors of 6 are: 1 2 3 6
🔍

Dry Run

Let's trace the input 6 through the code to find its factors.

1

Input number

User enters number = 6

2

Start loop

Loop variable i starts at 1

3

Check divisibility

Check if 6 % i == 0 for i = 1 to 6

4

Print factors

Print i when condition is true: 1, 2, 3, 6

5

End loop

Loop ends after i = 6

i6 % iIs factor?
10Yes
20Yes
30Yes
42No
51No
60Yes
💡

Why This Works

Step 1: Loop through numbers

We use a for loop to check every number from 1 to the input number because factors must be within this range.

Step 2: Check divisibility

Using number % i == 0 checks if the remainder is zero, meaning i divides the number exactly.

Step 3: Print factors

When the condition is true, we print i as a factor because it divides the number evenly.

🔄

Alternative Approaches

Using while loop
java
import java.util.Scanner;

public class FactorsWhile {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = sc.nextInt();
        int i = 1;
        System.out.print("Factors of " + number + " are: ");
        while (i <= number) {
            if (number % i == 0) {
                System.out.print(i + " ");
            }
            i++;
        }
        sc.close();
    }
}
Uses a while loop instead of for loop; functionally same but shows different loop style.
Optimized up to square root
java
import java.util.Scanner;

public class FactorsOptimized {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = sc.nextInt();
        System.out.print("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) + " ");
                }
            }
        }
        sc.close();
    }
}
Checks factors only up to square root for better performance, printing both factors at once.

Complexity: O(n) time, O(1) space

Time Complexity

The program checks every number from 1 to n, so it runs in linear time O(n).

Space Complexity

It uses only a few variables and prints output directly, so space is constant O(1).

Which Approach is Fastest?

The optimized approach checking up to square root of n reduces time to O(√n), which is faster for large numbers.

ApproachTimeSpaceBest For
Simple for loopO(n)O(1)Small to medium numbers
While loopO(n)O(1)Same as for loop, different style
Optimized sqrt loopO(√n)O(1)Large numbers for better performance
💡
Always check divisibility using number % i == 0 to find factors.
⚠️
Beginners often forget to include the number itself as a factor or start the loop from 0 causing division errors.