Check Natural Number in C#

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 2 Code Examples
integer checks

What you’ll learn

  • What natural numbers mean in this tutorial: positive integers 1, 2, 3, ….
  • How to test with n > 0 using a reusable IsNaturalNumber helper.
  • 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.WriteLine and optional Console.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.

Natural 1, 2, 3…
Not natural 0, negatives
Test n > 0

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.

Integer

Try 0 or -1 to see “not natural”.

Live result
Press “Check”.

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

Pseudocode
function isNatural(n):
    return n > 0
1

Check one integer (reference)

Tests a fixed value 42 with IsNaturalNumber and prints the result — matches the classic reference output.

c#
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.

2

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.

c#
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

In this page, a natural number is a positive integer used for counting: 1, 2, 3, and so on.
It depends on the textbook. Here we use n > 0, so 0 is not natural.
No. They fail the n > 0 test.
This topic is about integers only. Values like 3.5 are not natural numbers.
IsNaturalNumber keeps the rule in one place — reusable in checks, loops, and tests.
Use Console.ReadLine() with int.TryParse so invalid text does not crash the program.
A single check is O(1). Printing every natural number from 1 to k is O(k).

🔄 Input / output examples

Input (n)Natural?Sample message
42Yes42 is a natural number.
1Yes1 is a natural number.
0No0 is not a natural number.
-7No-7 is not a natural number.

Edge cases and pitfalls

Definitions vary in math texts about zero; this page consistently uses n > 0.

Zero

Is 0 natural?

Some authors include 0; this tutorial does not. State your convention in exams and interviews.

Input

Non-integer text

Use int.TryParse — do not assume ReadLine always returns valid digits.

Range

Negative start

If the loop starts below 1, IsNaturalNumber filters correctly — nothing prints for negatives or zero.

⏱️ Time and space complexity

TaskTimeExtra space
Single IsNaturalNumber checkO(1)O(1)
Print naturals from 1 to kO(k)O(1)

Summary

  • Rule: natural ⇔ n > 0 for integers on this page.
  • C#: IsNaturalNumber returns bool; use in if or loops.
  • Complexity: O(1) per check; O(k) to list 1..k.
Did you know?

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).

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