- Factors
2 × 6,3 × 4, etc.
Check Composite Number in C#
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 everyibelown. - How to print all composites in a small range (like
1to10), 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(), andConsole.WriteLine.forloops, 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.
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 = 35You 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
- Divisors
1and7only
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.
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
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 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.
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.
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.
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
🔄 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())).
| n | Composite? | Note |
|---|---|---|
12 | Yes | Shares factors such as 2 and 3 |
7 | No | Prime |
1 | No | Neither prime nor composite |
4 | Yes | Smallest composite |
Edge cases and pitfalls
These trip people up even when the math is understood.
Smallest prime
No divisor appears between 1 and 2; return not composite. Accidentally marking 2 as composite is a common bug.
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.
Perfect squares
9, 16, 25, … are composite because √n is an integer factor.
i * i <= n
Multiplying large i values can overflow int before comparing; stick with i <= n / i for positive n.
⏱️ Time and space complexity
| Method | Time (single n) | Extra space |
|---|---|---|
Naive trial to n − 1 | O(n) | O(1) |
Trial to √n (i ≤ n / i) | O(√n) | O(1) |
Range [a, b] with √ test each value | O((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 > 1and not prime;1is neither. - Code: trial division; tighten loops with
i <= n / iwhen you care about speed. - Watch-outs: label
n = 2correctly, remembern = 1is special, and avoid overflow fromi * i.
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.
8 people found this page helpful
