Check Composite Number in C#

Beginner
⏱️ 10 min read
📚 Updated: May 2026
🎯 2 Code Examples
Number theory

What you’ll learn

  • What composite means and how it differs from prime (and why 1 is neither).
  • A fast single check using trial division up to √n, plus an easy-to-read loop that tries every i below n.
  • How to print all composites in a small range (like 1 to 10), with a live preview, FAQs, and complexity notes.

Overview

Think of a whole number n bigger than 1. If you can split n into two smaller whole factors (both bigger than 1), then n is composite. If the only factors are 1 and n, then n is prime. This page gives two C# styles: a tight √n loop for one value, then a plain loop for classroom-style range printing.

Two programs

Example 1: one number with a short trial loop. Example 2: composites between 1 and 10 using a very literal divisibility scan.

Live preview

Type an integer and see composite, prime, or neither for values n ≤ 1—no compile step.

Clear wording

Some older pages describe composites in a fuzzy way. Here we stick to the usual rule: n > 1 and not prime.

Prerequisites

You should be comfortable with tiny C# programs before running the samples.

  • using System;, class Program, static void Main(), and Console.WriteLine.
  • for loops, integer division, and % (remainder) to test divisibility.

What is a composite number?

An integer n > 1 is composite when it is not prime. Another equivalent wording: n has more than two positive divisors if you count 1 and n (for example 12 is divisible by 2, 3, 4, 6, and others).

The number 1 only has one positive divisor, so mathematicians put it in a third bucket: neither prime nor composite.

Prime exactly two divisors
Composite > two divisors
Unit n = 1

Why √n is enough

If n > 1 breaks apart as n = d · k with both pieces at least 2, then the smaller piece cannot exceed √n. So searching for a divisor only from 2 up to ⌊√n⌋ catches every composite.

n = 35

You might spot 5 × 7. The factor 5 is below √35 (about 5.92). Once you find one factor in that range, you already know n is composite.

Quick examples

12 Composite
Factors
2 × 6, 3 × 4, etc.
7 Prime
Divisors
1 and 7 only

Memory trick: finding one honest factor between 1 and n is enough to shout “composite!” Proving prime means no factor showed up when you searched correctly.

Live preview

Enter any integer that fits in JavaScript’s safe integer range. For n ≤ 1 we explain that usual definitions only label composites among integers greater than 1.

Huge values may feel sluggish in the browser; the sample C# programs assume ordinary int-sized homework numbers.

Live result
Press “Check” to classify n and see why: for composites we show a divisor and n = d × (n/d); for primes we summarize trial division up to √n.

Algorithm

Goal: answer “Does n have any divisor strictly between 1 and n?”

Guard small values

If n ≤ 1, return not composite under the usual textbook definition.

Trial division

Try candidates i starting at 2. Stop when i · i > n (same idea as i ≤ n / i in code) or, in the naive version, when i reaches n − 1.

Decide

If some i divides n evenly (n % i == 0), return composite. If the loop finishes with no hits, n is prime.

📜 Pseudocode

Pseudocode
function isComposite(n):
    if n ≤ 1:
        return false
    for i from 2 while i * i ≤ n:
        if n mod i = 0:
            return true
    return false
1

Single-number check with a √n trial loop

Uses i <= number / i instead of Math.Sqrt so everything stays integer-only and matches what interviewers like to hear.

c#
using System;

class Program
{
    static bool IsComposite(int number)
    {
        if (number <= 1)
        {
            return false;
        }

        for (int i = 2; i <= number / i; i++)
        {
            if (number % i == 0)
            {
                return true;
            }
        }

        return false;
    }

    static void Main()
    {
        int testNumber = 12;

        if (IsComposite(testNumber))
        {
            Console.WriteLine($"{testNumber} is a composite number.");
        }
        else
        {
            Console.WriteLine($"{testNumber} is not a composite number.");
        }
    }
}

Explanation

As soon as any trial i divides number, we return true. If the loop completes, every potential factor pair was ruled out, so the value is prime and not composite.

for (int i = 2; i <= number / i; i++)

Integer √ trick. For positive number, the condition i <= number / i is equivalent to i · i ≤ number without multiplying i by itself (helps avoid overflow in some languages).

if (number <= 1)

Stay aligned with definitions. Zero, negatives (if you ever passed them in), and 1 are treated as not composite here.

2

Naive check plus composites from 1 to 10

This mirrors the classic homework style: try every i from 2 up to n − 1. It is slower on huge numbers but easy to read. The printed banner matches the console line exactly.

c#
using System;

class Program
{
    static bool IsComposite(int number)
    {
        if (number < 2)
        {
            return false;
        }

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

        return false;
    }

    static void Main()
    {
        Console.WriteLine("Composite numbers in the range 1 to 10 are:");

        for (int i = 1; i <= 10; i++)
        {
            if (IsComposite(i))
            {
                Console.Write($"{i} ");
            }
        }

        Console.WriteLine();
    }
}

Explanation

Outer loop walks each candidate i; inner loop asks whether anything between 2 and i − 1 divides i.

for (int i = 2; i < number; i++)

Literal scan. No shortcuts yet—fine for small labs. Swap in Example 1’s body when a teammate asks about speed.

Optimization

Tight loop bound. Prefer stopping near √n (Example 1) instead of marching all the way to n − 1.

Bulk checks. Need many answers? A sieve builds tables faster than repeating trial division for every query.

Interview: define composite clearly, guard n ≤ 1, then mention the √ observation.

❓ FAQ

A composite number is an integer n strictly greater than 1 that is not prime: there is some divisor d with 1 < d < n, so n splits into smaller whole-number factors. Examples: 4, 6, 8, 9, 10.
No. By the usual definition, 1 is neither prime nor composite.
No. Two is prime; nothing divides it evenly between 1 and 2.
If n = d×k with both factors ≥ 2, the smaller factor is at most √n. So any composite n has a divisor in [2, ⌊√n⌋]. Trying every i up to n−1 works too; it is just slower.
Primality and compositeness are defined for integers greater than 1. Simple interview programs assume nonnegative or positive input.
Checking one n with trial up to √n is O(√n) time and O(1) extra space. Scanning every integer in [a,b] multiplies that cost.

🔄 Input / output examples

Swap testNumber in Example 1 or widen the for loop in Example 2 to experiment interactively (optional: read with int.Parse(Console.ReadLine())).

nComposite?Note
12YesShares factors such as 2 and 3
7NoPrime
1NoNeither prime nor composite
4YesSmallest composite

Edge cases and pitfalls

These trip people up even when the math is understood.

n = 2

Smallest prime

No divisor appears between 1 and 2; return not composite. Accidentally marking 2 as composite is a common bug.

n = 1

Neither bucket

Saying “not composite” is correct but does not automatically mean prime; spell that out in UI copy if users might confuse the two.

Squares

Perfect squares

9, 16, 25, … are composite because √n is an integer factor.

Overflow

i * i <= n

Multiplying large i values can overflow int before comparing; stick with i <= n / i for positive n.

⏱️ Time and space complexity

MethodTime (single n)Extra space
Naive trial to n − 1O(n)O(1)
Trial to √n (i ≤ n / i)O(√n)O(1)
Range [a, b] with √ test each valueO((b − a + 1)·√b)O(1)

Both sample programs only store a handful of integers: auxiliary space stays constant aside from the call stack.

Summary

  • Definition: composite means n > 1 and not prime; 1 is neither.
  • Code: trial division; tighten loops with i <= n / i when you care about speed.
  • Watch-outs: label n = 2 correctly, remember n = 1 is special, and avoid overflow from i * i.
Did you know?

The integer 1 is neither prime nor composite. The smallest composite is 4 (2 × 2). Every composite n has a divisor d with 2 ≤ d ≤ √n, which is why trying divisors only up to √n is enough.

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.

8 people found this page helpful