Definition
n > 0
Counting numbers 1, 2, 3, … — zero is out under our rule.
Under this tutorial’s definition, a natural number is a positive integer: n > 0. This page covers the one-comparison check, printing a counting range, CLI validation, a live preview, worked PHP examples, edge cases, and complexity.
n > 0
Counting numbers 1, 2, 3, … — zero is out under our rule.
isNatural
Return true when the integer is strictly positive.
Sample 42
Print a clear sentence for one fixed value.
1..10
Print a slice of counting numbers in order.
Try values
Check 42, 0, or -3 under the same rule.
O(k) range
One comparison for a single test; linear to print a range.
Natural numbers are the counting numbers you use for “how many?” — 1, 2, 3, and so on. In this lesson we treat natural as the same as positive integer: the integer must be strictly greater than zero.
Some textbooks include 0. Your program must follow the definition your teacher or spec states. Here the code uses $num > 0, so zero and negatives fail the test.
It is a classic validation warm-up: state a definition, encode it as a comparison, and discuss edge cases like zero in interviews or exams.
Integer with $n > 0.
State whether 0 counts before coding.
Single check or print a counting range.
Decimals are out of scope for this page.
In short: for an integer $num, answer yes when $num > 0; otherwise no — and say so clearly in the output.
Given an integer, decide whether it is natural under the rule num > 0, and optionally print counting numbers in a range.
// 42 -> natural (42 > 0)
// 0 -> not natural under n > 0
// -3 -> not natural | Item | Type | Description |
|---|---|---|
$num | int | Integer to classify. |
| Return | bool | true if natural under n > 0. |
| Printed output | text | Yes/no sentence, or a printed range. |
function isNatural(num): // integer
if num > 0:
return true
return false | Definition | Test | Includes 0? |
|---|---|---|
| This page (school-style) | $n > 0 | No |
| Some math texts | $n >= 0 | Yes |
| Positive integer only | $n > 0 | Same as this page |
| Goal | Pattern |
|---|---|
| Core test | return $num > 0; |
| Include zero | return $num >= 0; (only if required) |
| Print message | echo $number . " is a natural number.\n"; |
| Print range | for ($i = $start; $i <= $end; $i++) |
| Filter in range | if ($i > 0) echo $i . " "; |
| CLI parse | $raw = trim(fgets(STDIN)); |
Same topic — different definitions and output shapes.
positive onlyThis page — 0 is not natural
include zeroUse only when the syllabus includes 0
1..10 listShow counting numbers without a yes/no
define 0Ask whether zero counts before coding
Reach for a natural-number check when input validation or counting-number talk matters.
Accept only positive whole numbers before other logic.
Definitions, comparisons, and edge talk about zero.
Print 1..n only when n is a natural count.
Pairs with even/odd and other integer checks.
Decimals need a different type and different rules.
Key benefit: a one-line definition that trains clear specs, boolean helpers, and edge-case discussion.
Uses the same rule as the code: natural means integer > 0.
Three complete PHP programs — single yes/no check, print 1 through 10, and CLI input with validation. Click View Output to reveal sample console results.
A reusable helper and one sample value.
Helper check with sample value 42 and a clear yes/no output line.
<?php
function isNatural(int $num): bool
{
return $num > 0;
}
$number = 42;
if (isNatural($number)) {
echo $number . " is a natural number.\n";
} else {
echo $number . " is not a natural number.\n";
}
?> Change $number to 0 or a negative value to see the other branch. The helper is a single comparison — easy to explain aloud in an interview.
Print a slice of the naturals without testing each value by hand.
Lists the counting numbers from 1 through 10 on one line.
<?php
function printNaturalsInRange(int $start, int $end): void
{
echo "Natural numbers in the range $start to $end:\n";
for ($i = $start; $i <= $end; $i++) {
if ($i > 0) {
echo $i . " ";
}
}
echo "\n";
}
printNaturalsInRange(1, 10);
?> The loop walks every integer in the closed range; the $i > 0 filter keeps only naturals under our definition. If $start could be below 1, that filter (or clamping) keeps the output honest.
Same helper; the value comes from STDIN with a numeric check.
Reads one integer, rejects non-numeric input, then classifies with isNatural.
<?php
function isNatural(int $num): bool
{
return $num > 0;
}
echo "Enter an integer: ";
$raw = trim(fgets(STDIN));
if (!is_numeric($raw) || strpos($raw, ".") !== false) {
echo "Please enter a whole number.\n";
exit(1);
}
$number = (int)$raw;
if (isNatural($number)) {
echo $number . " is a natural number.\n";
} else {
echo $number . " is not a natural number.\n";
}
?> Validate before casting so decimals and letters do not silently become integers. Under our definition, typed 0 correctly prints “not a natural number.”
Use a literal, or read CLI input and validate it.
If $num > 0, it is natural under this lesson’s rule.
Print a yes/no sentence a human can read.
The value is labeled natural or not under the chosen definition.
Apply $num > 0 to a few integers.
$num | Test | Result |
|---|---|---|
42 | 42 > 0 | natural |
1 | 1 > 0 | natural |
0 | 0 > 0 | not natural (here) |
-3 | -3 > 0 | not natural |
If your course includes zero, change the test to >= and update the wording together.
Where natural-number checks show up beyond the interview prompt.
Accept only positive whole counts.
Example: number of items > 0.
State whether 0 is included, then code.
Example: > vs >= choice.
Print 1..n as counting numbers.
Example: Example 2 pattern.
Ensure n is natural before factorial or tables.
Example: reject 0 for some prompts.
O(1) for one test; O(k) to print k values.
Example: range length end-start+1.
Practice returning true/false cleanly.
Example: isNatural($n).
Pro Tip: open with “I’ll treat natural as n > 0 unless you include zero” before writing the comparison.
Why this tiny check still earns interview points.
One comparison encodes the whole definition.
Flip to >= if the syllabus includes zero.
Reuse isNatural before loops, tables, or factorials.
O(1) time and space for a single classification.
Pro Tip: keep the helper boolean and put human wording in the echo layer, not inside the math.
Small habits that keep natural-number solutions interview-ready.
Say whether zero counts before writing > or >=.
Return true/false; print messages outside.
Reject non-integers before casting.
If $start can be ≤ 0, filter or clamp.
Always dry-run 0 under your chosen definition.
Pro Tip: check 42, 0, and -3 aloud — those three cases catch almost every beginner mistake.
Mistakes that commonly break natural-number solutions.
Assuming every teacher includes or excludes 0.
→ Align the comparison with the stated definition.
Casting 1.5 to 1 without complaining.
→ Validate whole-number input on CLI paths.
Looping from a negative start without filtering.
→ Keep the $i > 0 guard (or clamp start).
Hard-coding “natural” strings inside every caller.
→ Centralize the boolean test in isNatural.
Including zero while the prompt said positive only.
→ Match operator and wording to the spec.
Handle these before calling the check done.
num == 0Not natural under n > 0; may be natural if your syllabus includes 0.
num < 0Always fails this lesson’s test.
num == 1Smallest natural under the school-style definition.
Still an integer; the comparison works. Confirm limits for your PHP runtime if needed.
Out of scope for int helpers — validate CLI input.
Filter with $i > 0 or clamp before printing.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
$n with $n > 0.isNatural.Quick Takeaway: natural here means $n > 0 — say so, test zero, and print a clear sentence.
| Operation | Time | Extra space |
|---|---|---|
| Single test | O(1) | O(1) |
| Print range start..end | O(end - start + 1) | O(1) |
| CLI parse + validate | O(1) | O(1) |
The interesting part is the definition, not the asymptotic cost.
Checking a natural number is a definition plus one comparison: under this page, return true when $num > 0. Extend the same helper to range printing and CLI validation without changing the core rule.
Practice the three examples above, then continue to number combinations.
State whether zero counts, then use > or >= — and test 0 explicitly.
isNatural helper$i > 0 when neededClassify counting numbers the interview-friendly way.
n > 0 here
DefinitionConfirm first
EdgeisNatural bool
CodePrint 1..k
PatternO(1) / O(k)
AnalysisIn many school-level texts, natural numbers start at 1 (1, 2, 3, ...). Some mathematicians include 0; programs must follow whichever definition your teacher or spec states — here we use n > 0.
Learn how to generate number combinations and pairs with nested loops in PHP.
8 people found this page helpful