Check Natural Number in C#
What you’ll learn
- What natural numbers mean in this tutorial: positive integers
1, 2, 3, …. - How to test with
n > 0using a reusableIsNaturalNumberhelper. - Two programs: check one value and list naturals from 1 to 10, plus a live preview.
Overview
Checking whether a number is natural is one of the simplest predicates in programming: a single comparison. It is a great stepping stone before harder number-property programs like even, prime, or Armstrong numbers.
One rule
n > 0 for integers on this page.
Live preview
Type an integer and see natural or not instantly.
Range demo
Print 1 through 10 using a loop.
Prerequisites
int variables, comparison operators, if / else, and basic loops.
- You can compare integers with
>,<, and==. - You are comfortable with
Console.WriteLineand optionalConsole.ReadLine.
What is a natural number?
Natural numbers are the counting numbers you use when you say “one, two, three” without fractions or negatives.
On this page, an integer n is natural if and only if n > 0. That means 1, 42, and 1000 qualify; 0 and -5 do not.
Quick examples
42 is natural (42 > 0). 0 is not (0 > 0 is false). -3 is not. Every integer from 1 to 10 in the range example is natural.
Live preview
Enter a whole integer and run the same n > 0 rule as the C# program.
Algorithm
Goal: decide whether integer n is natural.
Read n
From a literal, variable, or Console.ReadLine with parsing.
Compare
Return true when n > 0; otherwise false.
Report
Print a clear message for the user.
📜 Pseudocode
function isNatural(n):
return n > 0 Check one integer (reference)
Tests a fixed value 42 with IsNaturalNumber and prints the result — matches the classic reference output.
using System;
class Program
{
static bool IsNaturalNumber(int num)
{
return num > 0;
}
static void Main()
{
int number = 42;
if (IsNaturalNumber(number))
{
Console.WriteLine($"{number} is a natural number.");
}
else
{
Console.WriteLine($"{number} is not a natural number.");
}
}
} Explanation
IsNaturalNumber returns a bool — ideal for if branches. Change number to 0 or -5 to see the else branch run.
Natural numbers from 1 to 10
Loops through a range and prints each value that passes IsNaturalNumber. For 1..10 every value qualifies — the helper still shows how to filter in wider ranges.
using System;
class Program
{
static bool IsNaturalNumber(int number)
{
return number > 0;
}
static void Main()
{
int startRange = 1;
int endRange = 10;
Console.WriteLine($"Natural Numbers in the range {startRange} to {endRange}:");
for (int i = startRange; i <= endRange; i++)
{
if (IsNaturalNumber(i))
{
Console.Write($"{i} ");
}
}
Console.WriteLine();
}
} Explanation
If you changed startRange to 0, the loop would still run eleven times but IsNaturalNumber(0) would skip printing zero — useful when the range might include non-naturals.
Beyond the basics
Console input. Read with int.TryParse(Console.ReadLine(), out int n) before calling IsNaturalNumber(n).
Zero debate. In interviews, state your definition: “I treat naturals as positive integers, so n > 0.”
No optimization needed for the predicate itself — it is already O(1).
❓ FAQ
🔄 Input / output examples
| Input (n) | Natural? | Sample message |
|---|---|---|
42 | Yes | 42 is a natural number. |
1 | Yes | 1 is a natural number. |
0 | No | 0 is not a natural number. |
-7 | No | -7 is not a natural number. |
Edge cases and pitfalls
Definitions vary in math texts about zero; this page consistently uses n > 0.
Is 0 natural?
Some authors include 0; this tutorial does not. State your convention in exams and interviews.
Non-integer text
Use int.TryParse — do not assume ReadLine always returns valid digits.
Negative start
If the loop starts below 1, IsNaturalNumber filters correctly — nothing prints for negatives or zero.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
Single IsNaturalNumber check | O(1) | O(1) |
Print naturals from 1 to k | O(k) | O(1) |
Summary
- Rule: natural ⇔
n > 0for integers on this page. - C#:
IsNaturalNumberreturnsbool; use inifor loops. - Complexity: O(1) per check; O(k) to list 1..k.
In this tutorial, natural numbers mean positive integers: 1, 2, 3, …. The check is simply n > 0 — so zero is not natural here (some math texts include 0; we state our convention clearly).
8 people found this page helpful
