C++ Composite Number Program

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

What Is a Composite Number?

A composite number is an integer greater than 1 that has at least one positive divisor other than 1 and itself (more than two positive divisors).

Remember
n is composite  ↔  n > 1  and  n has a divisor d with 1 < d < n

Buckets for positive integers:
  n = 1      → neither prime nor composite
  prime      → exactly two divisors (1 and n)
  composite  → more than two divisors

Smallest composite: 4 (= 2 × 2)
nDivisorsClass
11Neither
21, 2Prime
41, 2, 4Composite
121, 2, 3, 4, 6, 12Composite

How to Check a Composite Number in C++

Reject n ≤ 1, then look for any divisor from 2 through √n.

  1. If n <= 1, return false (not composite).
  2. Try every i from 2 while i * i <= n.
  3. If any n % i == 0, return true.
  4. Otherwise return false (prime, for n > 1).
MethodIdeaTimeBest for
√n trial divisionFind any factor ≤ √nO(√n)Interviews, default
Range scanReuse the helperO(N√N)Listing composites
Witness factorReturn the first divisorO(√n)Explain why

Pseudocode

Pseudocode
function IsComposite(n):
    if n <= 1:
        return false
    for i from 2 to floor(sqrt(n)):
        if n mod i == 0:
            return true
    return false

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Testn > 1 and has a factor in 2..√n
1Neither prime nor composite
Smallest4
1..104, 6, 8, 9, 10
Loop boundi * i <= n (integer-safe)

Live Preview

Enter a positive integer to see whether it is composite, prime, or neither.

Positive whole numbers. Tap a chip or type a value.

Result
n = 12
Divisible by 2
Verdict: COMPOSITE

Worked Walkthrough — n = 12

Look for any factor from 2 through √12 ≈ 3.46.

Calculation
n = 12  (n > 1 ✓)
Try i = 2 .. floor(√12) = 3

i = 2:  12 % 2 == 0  ✓

Found a factor → composite
(12 = 2 × 6)

C++ Programs

Start with a boolean √n check. Then list a range, and finish by returning a witness factor.

Example 1 — Check One Number

Short function, fast early return, and integer-safe loop bound.

C++
#include <iostream>

bool isComposite(int n) {
    if (n <= 1) {
        return false;
    }
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return true;
        }
    }
    return false;
}

int main() {
    int n = 12;
    if (isComposite(n)) {
        std::cout << n << " is a composite number.\n";
    } else {
        std::cout << n << " is not a composite number.\n";
    }
    return 0;
}

How It Works

  1. Return false immediately when n <= 1.
  2. Loop while i * i <= n (avoids floating sqrt).
  3. Any divisor found means composite — return true.
  4. If the loop finishes, n has no nontrivial factor ≤ √n.
  5. For 12, i = 2 divides immediately.

Example 2 — Composite Numbers from 1 to 10

Print only values that satisfy the composite test.

C++
#include <iostream>

bool isComposite(int n) {
    if (n <= 1) {
        return false;
    }
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return true;
        }
    }
    return false;
}

int main() {
    std::cout << "Composite numbers in the range 1 to 10 are:\n";
    for (int num = 1; num <= 10; num++) {
        if (isComposite(num)) {
            std::cout << num << " ";
        }
    }
    std::cout << "\n";
    return 0;
}

How It Works

  1. Reuse the same √n helper for every value.
  2. Loop from 1 to 10 and print matches.
  3. 1 is skipped (neither); 2, 3, 5, 7 are prime.
  4. Composites printed: 4, 6, 8, 9, 10.
  5. Useful self-check when rewriting the function.

Example 3 — Find a Witness Factor

If composite, report one divisor that proves it.

C++
#include <iostream>

/** Return a nontrivial divisor, or -1 if not composite. */
int firstFactor(int n) {
    if (n <= 1) {
        return -1;
    }
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return i;
        }
    }
    return -1;
}

int main() {
    int samples[] = { 12, 7, 1, 9 };
    int count = sizeof(samples) / sizeof(samples[0]);
    for (int i = 0; i < count; i++) {
        int n = samples[i];
        int f = firstFactor(n);
        if (f == -1) {
            const char* label = n <= 1 ? "neither" : "prime";
            std::cout << n << ": not composite (" << label << ")\n";
        } else {
            std::cout << n << ": composite (divisible by " << f << ")\n";
        }
    }
    return 0;
}

How It Works

  1. Same loop as isComposite, but return the divisor.
  2. Return -1 when no nontrivial factor exists.
  3. Label n <= 1 as neither; otherwise as prime.
  4. 12 yields factor 2; 9 yields factor 3.
  5. Handy when the follow-up is “show me a factor.”

Edge Cases & Pitfalls

Check these before calling the solution done.

n = 1

Neither prime nor composite

Always return false for composite. Do not call 1 prime either.

2, 3

Small primes

The √n loop never runs (or finds no factor). Correctly not composite.

Bound

Use i * i <= n

Safer than casting sqrt to int. Avoids floating-point off-by-ones.

Negatives

Usually not classified

Composite/prime definitions apply to integers greater than 1.

Time and Space Complexity

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

Worst case is a prime (or 1): the loop runs through all candidates up to √n.

Key Takeaways

  • Definition: n > 1 with a factor other than 1 and n.
  • Smallest: 4.
  • Check: trial division with i * i <= n.
  • 1: neither prime nor composite.

One line: if n > 1 and any factor ≤ √n exists, it is composite.

Frequently Asked Questions

An integer greater than 1 that has at least one positive divisor other than 1 and itself. The smallest composite number is 4.
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.
If n has a factor larger than √n, it also has a paired factor smaller than √n.
The optimized check is O(√n) time and O(1) extra space.

Next: Condense a Number

Keep summing digits until only one digit remains (digital root).

Condense a number tutorial →

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.

18 people found this page helpful