Check Natural Number in Java
What you’ll learn
- How to check natural number using
n > 0. - How to build reusable helper
isNatural. - How to print 1 to 10 as natural numbers.
Prerequisites
Java integers, comparison operators, and loops.
- Know integer comparisons like
n > 0. - Comfortable with simple loops and console input/output.
The idea
Natural number check is one rule: an integer is natural if and only if n > 0.
Live preview
Algorithm
Read integer n; return natural when n > 0.
📜 Pseudocode
Pseudocode
if n > 0:
natural
else:
not natural1
Check one integer
java
public class Main {
static boolean isNatural(int num) {
return num > 0;
}
public static void main(String[] args) {
int number = 42;
if (isNatural(number)) {
System.out.println(number + " is a natural number.");
} else {
System.out.println(number + " is not a natural number.");
}
}
}2
Print natural numbers 1 to 10
java
public class Main {
static void printNaturalRange(int start, int end) {
System.out.println("Natural numbers in the range " + start + " to " + end + ":");
for (int i = start; i <= end; i++) {
System.out.print(i + " ");
}
System.out.println();
}
public static void main(String[] args) {
printNaturalRange(1, 10);
}
}Optimization
No algorithmic optimization needed; this is a direct constant-time predicate.
❓ FAQ
In this page, a natural number is a positive integer: 1, 2, 3, and so on.
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. Decimals are not natural numbers.
It makes code reusable and easy to test.
Single check is O(1). Printing range 1..k is O(k).
🔄 Input / output examples
1 -> natural, 10 -> natural, 0 -> not natural.
Edge cases
Definitions vary in math texts about zero; this page consistently uses n > 0.
⏱️ Time and space complexity
Single check is O(1); range printing from 1..k is O(k).
Summary
- Natural number check in Java is
n > 0. - Single check is
O(1).
Did you know?
In this tutorial, natural numbers mean positive integers: 1, 2, 3, .... So the check is simply n > 0.
8 people found this page helpful
