beginnerNumber ProblemsJava

Find All Factors of a Number

Print every positive divisor of a number in ascending order.

Quick Answer

A factor of n is any positive integer that divides n with no remainder. Loop i from 1 to n and collect every i where n % i == 0. For 12 the factors are 1, 2, 3, 4, 6 and 12.

Problem Statement

Given a positive integer n, list all of its factors (divisors) in ascending order. A factor is any positive integer that divides n with no remainder, including 1 and n itself.

For example, the factors of 12 are 1, 2, 3, 4, 6, 12.

Input: A single positive integer n.

Output: The factors of n in ascending order, separated by spaces.

Examples

Example 1
Input:  12
Output: 1 2 3 4 6 12

Each of these divides 12 evenly; 5, 7, 8, ... do not.

Example 2
Input:  7
Output: 1 7

7 is prime, so only 1 and 7 divide it.

Constraints

  • n >= 1 and fits in a 32-bit int
  • 1 and n are both counted as factors

Think Before You Code

Reveal the questions to ask yourself first
  • Which candidate divisors do you need to test, and up to what value?
  • What condition confirms that i is a factor of n?
  • Could you find factors in pairs to reduce the number of checks?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
A number `i` is a factor of `n` when `n % i == 0`.
Hint 2
Testing every `i` from 1 to `n` is the straightforward approach.
Hint 3
For speed, iterate only to sqrt(n) and add both `i` and `n / i` for each hit.

Approach

Reveal the step-by-step approach

Test each candidate divisor in order.

  1. Loop i from 1 to n.
  2. Whenever n % i == 0, record i as a factor.
  3. Output the collected factors in the order found (which is ascending).

This runs in O(n). A faster O(sqrt n) version loops only to sqrt(n) and, for each divisor i, also records the paired divisor n / i.

Dry Run

Walk through the example step by step

Finding the factors of n = 12:

i  | 12 % i | factor?
---+--------+--------
1  | 0      | yes -> 1
2  | 0      | yes -> 2
3  | 0      | yes -> 3
4  | 0      | yes -> 4
5  | 2      | no
6  | 0      | yes -> 6
...| ...    | ...
12 | 0      | yes -> 12

factors: 1 2 3 4 6 12

Solution

Reveal the full Java solution
public class FindFactors {
    public static String factors(int n) {
        int num = Math.abs(n);
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= num; i++) {
            if (num % i == 0) {
                if (sb.length() > 0) {
                    sb.append(" ");
                }
                sb.append(i);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(factors(12)); // 1 2 3 4 6 12
        System.out.println(factors(7));  // 1 7
    }
}

Every candidate i from 1 to n is tested with a single remainder check, and those that divide n evenly are the factors. Because i increases, the factors come out already sorted. The StringBuilder is only used to format the output; the divisor logic itself is pure arithmetic.

Time: O(n) for the simple loop (O(sqrt n) with divisor pairing)Space: O(1) beyond the output

Common Mistakes

  • Starting the loop at 2 and losing 1 as a factor, or stopping before n and losing n.
  • When using the sqrt optimisation, adding a perfect-square root twice.

Edge Cases to Test

  • n = 1 has a single factor: 1.
  • A prime number returns exactly two factors, 1 and itself.

Interview Follow-Ups

  • How would the O(sqrt n) version avoid double-counting when n is a perfect square?
  • How would you return only the prime factors instead of all factors?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us