Check Natural Number in C++

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code Examples
Integers

What you’ll learn

  • A down-to-earth meaning of natural number (counting numbers) and the test n > 0 on integers.
  • A tiny helper is_natural plus int main() with a clear std::cout 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, std::cout, and integer types.

  • #include <iostream> and writing int main().
  • 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.

Try 42, 0, or -3. Non-integers show a short error message.

Live result
Press “Check”.

Algorithm

Goal: decide whether num is a natural number under the rule num > 0.

Read or fix num

Use a literal (example 1) or later std::cin when you wire input.

Test

If num > 0, answer yes; otherwise no.

Print

Print a sentence a human can read.

📜 Pseudocode

Pseudocode
function is_natural(num):   // integer
    if num > 0:
        return true
    return false
1

Yes / no for one integer

Same structure as the reference: helper returns 1 or 0, int main(), and sample value 42.

c++
#include <iostream>

int is_natural(int num) {
    return (num > 0) ? 1 : 0;
}

int main() {
    int number = 42;

    if (is_natural(number)) {
        std::cout << number << " is a natural number.\n";
    } else {
        std::cout << number << " is not a natural number.\n";
    }

    return 0;
}

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.

2

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.

c++
#include <iostream>

void print_integers_in_range(int start, int end) {
    std::cout << "Natural numbers in the range " << start << " to " << end << ":\n";

    for (int i = start; i <= end; ++i) {
        std::cout << i << " ";
    }

    std::cout << "\n";
}

int main() {
    int start = 1;
    int end = 10;

    print_integers_in_range(start, end);
    return 0;
}

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 std::cin, validate input and reject non-numeric text before calling is_natural.

❓ FAQ

In this tutorial, it is a whole number you get when you count real things: 1, 2, 3, and so on. We treat “natural” as the same as “positive integer” (strictly greater than zero).
It depends on the book. Some definitions include 0; this program uses the test n > 0, so 0 is not natural here. If your course includes 0, change the test to n >= 0 and rename the message text to match your definition.
Classic C has no built-in bool in very old standards. Returning 1 for “yes” and 0 for “no” is a small, clear habit. With C99 you can use stdbool.h if you prefer.
The function takes an int. Numbers like 1.5 are not integers, so you would use a different type and different rules (this page stays with whole numbers).
They fail the n > 0 test, so the program says they are not natural numbers under our definition.
A single comparison is O(1) time. Printing a range from a to b costs O(b - a + 1) time for the loop body.

🔄 Input / output

Both programs use fixed values to keep output predictable. Swap in std::cin when you want the user to type the test value or the ends of a range.

Edge cases

Zero

num == 0

Not natural under n > 0; may be natural if your syllabus includes 0—adjust the test and wording together.

Overflow

Very large int

Still an integer; the comparison works. If you need bigger values, consider long long.

⏱️ Time and space complexity

OperationTimeExtra space
Single testO(1)O(1)
Print range start..endO(end - start + 1)O(1)

Summary

  • Definition used: integer n with n > 0.
  • Patterns: one-off check plus optional range listing.
  • Remember: align code with the definition your exam uses (especially for 0).
Did you know?

In many school-level and ISO convention 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.

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