PHP Composite Number Program

Beginner
6 min read
Updated: Oct 2026
3 programs
Live preview

What Is a Composite Number?

A composite number is an integer n > 1 that has more than two positive divisors — at least one factor other than 1 and n itself. The smallest composite number is 4.

Remember
n is composite  ⇔  n > 1  and  n is not prime
                ⇔  n has a divisor d with 1 < d < n

Examples:
  4  = 2 × 2     → composite
  9  = 3 × 3     → composite
  15 = 3 × 5     → composite
  7              → prime (not composite)
  1              → neither prime nor composite
ClassConditionExample
Neithern <= 11, 0, negatives
PrimeExactly two divisors: 1 and n2, 3, 5, 7
CompositeMore than two positive divisors4, 6, 8, 9

First composite numbers (1–30)

List
4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30

How to Check a Composite Number in PHP

Four clear steps — then reuse the helper for ranges or a witness factor.

  1. If n <= 1, return false (not composite).
  2. Search for a divisor starting at 2 up to √n.
  3. Stop early when a divisor is found — the number is composite.
  4. If no divisor appears, the number is prime (not composite).
MethodIdeaTimeBest for
√n checkTest divisors from 2 to √nO(√n)Interviews, default
Range scanReuse the helper over 1 … NO(N√N)Listing composites
Witness factorReturn the first divisor foundO(√n)Explaining why it is composite

Pseudocode

Pseudocode
function IsComposite(n):
    if n <= 1:
        return false

    i = 2
    while i * i <= n:
        if n mod i == 0:
            return true          // found a factor
        i = i + 1

    return false                // prime

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Definitionn > 1 with a factor other than 1 and n
Smallest4
Not composite1, primes, and n <= 1
Fast limitLoop while i * i <= n
1 to 104, 6, 8, 9, 10
Classic check9 → composite; 7 → not

Live Preview

Enter any integer to see its classification and a factor if it is composite.

Whole numbers (capped at ±999999). Tap a chip or type a value.

Result
n = 9
Factor found: 3 (because 9 = 3 × 3)
Divisor count > 2
Verdict: COMPOSITE

Worked Walkthrough — n = 9

Start at 2. As soon as one factor appears, the number is composite.

Calculation
n = 9  (n > 1, so it could be prime or composite)

i = 2 → 9 % 2 != 0  → continue
i = 3 → 9 % 3 == 0  → factor found

9 = 3 × 3
Positive divisors: 1, 3, 9  (three divisors)
Result: composite

PHP Programs

Start with an O(√n) check for one number. Then list a range, and finish by returning a witness factor.

Example 1 — Check One Number

Look for any divisor from 2 while i * i <= n.

php
<?php
function isComposite(int $number): bool
{
    if ($number <= 1) {
        return false;
    }

    $i = 2;
    while ($i * $i <= $number) {
        if ($number % $i === 0) {
            return true;
        }
        $i++;
    }

    return false;
}

$n = 12;
if (isComposite($n)) {
    echo "$n is a composite number.\n";
} else {
    echo "$n is not a composite number.\n";
}

How It Works

  1. Reject n <= 1 immediately — those values are not composite.
  2. Loop while $i * $i <= $number (same as $i <= √n, no floating point).
  3. If $number % $i === 0, a factor exists between 1 and n, so return true.
  4. If the loop finishes with no hit, n is prime, so return false.
  5. For 12, $i = 2 divides evenly, so the method reports composite.

Example 2 — Composites from 1 to 10

Reuse the helper and print every composite in a closed range.

php
<?php
function isComposite(int $number): bool
{
    if ($number <= 1) {
        return false;
    }

    for ($i = 2; $i * $i <= $number; $i++) {
        if ($number % $i === 0) {
            return true;
        }
    }
    return false;
}

echo "Composite numbers in the range 1 to 10 are:\n";
$parts = [];
for ($num = 1; $num <= 10; $num++) {
    if (isComposite($num)) {
        $parts[] = $num;
    }
}
echo implode(' ', $parts) . "\n";

How It Works

  1. Loop from 1 to 10 inclusive.
  2. Call isComposite($num) for each value.
  3. Collect matches, then print with implode.
  4. Within 1…10 you should see 4, 6, 8, 9, and 10.
  5. The helper stays pure; the loop only decides what to print.

Example 3 — Witness Factor

Return the first divisor found — useful when you want to explain why a number is composite.

php
<?php
function firstFactor(int $number): ?int
{
    if ($number <= 1) {
        return null;
    }

    $i = 2;
    while ($i * $i <= $number) {
        if ($number % $i === 0) {
            return $i;
        }
        $i++;
    }
    return null;
}

$samples = [12, 7, 1, 9];
foreach ($samples as $n) {
    $f = firstFactor($n);
    if ($f === null) {
        $label = $n <= 1 ? 'neither' : 'prime';
        echo "$n: not composite ($label)\n";
    } else {
        echo "$n: composite (divisible by $f)\n";
    }
}

How It Works

  1. Same √n search as isComposite, but return the factor instead of true.
  2. null means no factor — the number is prime or <= 1.
  3. 12 is divisible by 2; 9 is divisible by 3.
  4. 7 has no factor up to √7 ≈ 2.6, so it is prime.
  5. 1 is neither prime nor composite.

Edge Cases & Pitfalls

Check these before calling the solution done.

n <= 1

Not composite

Return false before any loop. 1 is neither prime nor composite.

2 and 3

Smallest primes

Both are prime. The loop never finds a factor, so return false.

Squares

Still composite

9, 25, 49 are composite even when the only middle factor repeats.

Negatives

Usually out of scope

Prime/composite talk is for integers greater than 1. Treat negatives as not composite.

Time and Space Complexity

ProgramTimeExtra space
Single check / witnessO(√n)O(1)
Range 1…NO(N√N)O(1)

Key Takeaways

  • Definition: n > 1 with a factor other than 1 and itself.
  • Opposite of prime: composite ↔ not prime and n > 1.
  • Fast check: loop while i * i <= n.
  • Test set: 4 and 9 true; 1, 2, and 7 false.

One line: if n > 1 and any divisor exists between 2 and √n, the number is composite.

Frequently Asked Questions

An integer greater than 1 that has at least one positive divisor other than 1 and itself. Equivalently, it has more than two positive divisors. The smallest composite number is 4.
Reject n <= 1. Then look for a divisor from 2 through √n. If any divisor is found, the number is composite.
No. The number 1 is neither prime nor composite.
A prime has exactly two positive divisors: 1 and itself. A composite has more than two. Both require n > 1.
Checking divisors up to √n is O(√n) with O(1) extra space. Listing composites in a range of size N costs O(N√N).

Next: Condense a Number

Repeatedly sum digits until a single digit remains.

Condense a number tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful