Check Natural Number in PHP
What you’ll learn
- A down-to-earth meaning of natural number (counting numbers) and the test
n > 0on integers. - A tiny helper
isNaturalwith a clear PHP output message. - A second program that prints 1 through 10 in one line, and a live preview to try your own value.
Overview
If you can answer “how many?” with 1, 2, 3, ... you are using natural numbers in the everyday sense. The program only needs to know: is this integer strictly positive?
Yes / no check
One comparison: num > 0 for the sample definition.
Range listing
Print 1 2 ... 10 as a second pattern.
Live preview
Type an integer and see natural or not under our rule.
Prerequisites
if, for, echo, and integer types.
- Basic PHP syntax and writing small reusable functions.
- Comfort reading comparisons like
>and<=.
The idea
Store the value as an int. If it is greater than zero, treat it as natural for this lesson. Zero and negatives fail the test; fractions are not integers, so they are out of scope here.
That single rule is enough for many homework-style “is it natural?” problems.
Live preview
Uses the same rule as the code: natural means integer > 0.
Algorithm
Goal: decide whether num is a natural number under the rule num > 0.
Read or fix num
Use a literal (example 1) or read CLI input with fgets(STDIN).
Test
If num > 0, answer yes; otherwise no.
Print a sentence a human can read.
📜 Pseudocode
function isNatural(num): // integer
if num > 0:
return true
return falseYes / no for one integer
Same structure as the reference: 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";
}
?>Explanation
Change number to 0 or a negative value to see the other branch. The ternary form is compact; an if (num > 0) return 1; return 0; style reads aloud the same way.
Print from 1 to 10 in order
Lists the counting numbers from 1 through 10 on one line - handy when you want to show a slice of the naturals without testing each value.
<?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);
?>Notes
Range printing. Example 2 assumes start and end make sense for your task. If start could be below 1, either clamp it or filter so you only print true naturals under your definition.
User input. When you read with fgets(STDIN), validate numeric input before calling isNatural.
❓ FAQ
🔄 Input / output
Both programs use fixed values to keep output predictable. You can use trim(fgets(STDIN)) when you want user input.
Edge cases
num == 0
Not natural under n > 0; may be natural if your syllabus includes 0 - adjust the test and wording together.
Very large int
Still an integer; the comparison works. If you need bigger values, confirm integer limits for your PHP runtime.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
| Single test | O(1) | O(1) |
Print range start..end | O(end - start + 1) | O(1) |
Summary
- Definition used: integer
nwithn > 0. - Patterns: one-off check plus optional range listing.
- Remember: align code with the definition your exam uses (especially for 0).
In 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.
8 people found this page helpful
