Java Whole Number Program

Beginner
5 min read
Updated: Nov 2026
2 programs
Live preview

What Is a Whole Number?

Whole numbers are the counting numbers plus zero. On this page an integer is whole when n >= 0 (that is, 0, 1, 2, 3, …). They are the natural numbers with 0 added.

Remember
n is whole  ↔  n is an integer and n >= 0

Examples:
   0  → whole
   1  → whole
  42  → whole
  -3  → not whole

Whole = Natural ∪ {0}   (when natural means n > 0)

Natural vs Whole: the Short Debate

Textbooks do not fully agree on whether 0 is a natural number. Many school syllabi say natural numbers start at 1 and use whole numbers for the set that starts at 0. Some mathematics and computer science books count 0 as natural too. The practical rule: state your definition, then code it. This site uses n > 0 for natural and n >= 0 for whole.

nNatural (n > 0)Whole (n >= 0)
5YesYes
0NoYes
-2NoNo

How to Check a Whole Number in Java

Three clear steps — then either classify one value or list a range.

  1. State the definition you will use (here: n >= 0).
  2. Compare the integer against zero and return a boolean.
  3. Optionally loop from 0 to N to print whole numbers in a range.
TaskIdeaTimeBest for
Single checkreturn n >= 0;O(1)Yes/no classification
Range printLoop i from 0 to NO(N)Listing whole numbers

Pseudocode

Pseudocode
function IsWhole(n):
    return n >= 0

function PrintWholes(limit):
    for i from 0 to limit:
        print i

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
This page rulen >= 0
Natural rule heren > 0 (excludes zero)
Set{0, 1, 2, 3, …}
ZeroWhole — the only difference from natural on this site
NegativesNever whole
Helper tipReturn boolean; let main print the message

Live Preview

Enter any integer to see whether it is whole (n >= 0) and whether it is also natural (n > 0).

Whole numbers only. Tap a chip or type a value.

Result
n = 0
Whole (n >= 0): true
Natural (n > 0): false
Verdict: WHOLE NUMBER

Worked Walkthrough — n = 5, 0, and -3

Apply the same comparison to a typical case, the zero boundary, and a negative.

Calculation
Case A: n = 5
  5 >= 0 ? yes → whole

Case B: n = 0
  0 >= 0 ? yes → whole (but 0 > 0 is no, so not natural here)

Case C: n = -3
  -3 >= 0 ? no → not whole

Java Programs

Start with a boolean helper for one value. Then print whole numbers from 0 to 10 with a simple loop.

Example 1 — Single Check

Return true when the integer is greater than or equal to zero.

Java
class WholeNumber
{
    static boolean isWhole(int n)
    {
        return n >= 0;
    }

    public static void main(String[] args)
    {
        int number = 0;
        if (isWhole(number))
        {
            System.out.println(number + " is a Whole Number.");
        }
        else
        {
            System.out.println(number + " is not a Whole Number.");
        }
    }
}

How It Works

  1. isWhole encodes the page rule in one comparison: n >= 0.
  2. No loop is needed — the answer is a single boolean.
  3. main calls the helper and prints a clear message.
  4. For 0, the check is true, so the program reports a whole number (it would fail the natural check).
  5. Swap in a negative value to see the not-whole branch.

Example 2 — Print Whole Numbers 0 to 10

Whole numbers start at 0, so a forward loop from 0 through 10 lists them.

Java
class WholeRange
{
    public static void main(String[] args)
    {
        System.out.println("Whole numbers from 0 to 10:");
        for (int i = 0; i <= 10; i++)
        {
            System.out.print(i + " ");
        }
    }
}

How It Works

  1. Start at i = 0 because whole numbers include zero.
  2. The loop condition i <= 10 keeps the end inclusive.
  3. Each pass prints the current whole number and a space.
  4. After i becomes 11, the loop stops.
  5. Start at 1 instead and you are listing natural numbers.

Edge Cases & Pitfalls

Check these before calling the solution done.

Zero

Zero is whole

Use >=, not >. Writing n > 0 turns this into the natural-number check.

Negatives

Always false

Negative integers are never whole numbers.

Decimals

Integers only

This page classifies int values. Floating-point input needs a different conversation.

Definitions

Say your rule

Some syllabi call 0, 1, 2, … natural numbers. Explain which definition you use in interviews.

Time and Space Complexity

ProgramTimeExtra space
Single checkO(1)O(1)
Print 0 to NO(N)O(1)

Key Takeaways

  • Definition: whole means n >= 0 on this page.
  • Check: one comparison is enough — no loops required.
  • Natural vs whole: the only difference is 0 (> vs >=).
  • Test set: 0 and 5 whole; −3 not whole.

One line: if n >= 0, it is a whole number — the natural numbers plus zero.

Frequently Asked Questions

A whole number is a nonnegative integer: 0, 1, 2, 3, and so on. The check is simply n >= 0.
Whole numbers include 0. On this site natural numbers start at 1 (n > 0), so whole numbers are the natural numbers plus zero. Some textbooks already include 0 in the natural numbers, so always state your definition.
Yes. Zero is the smallest whole number and the one value that separates whole from natural under the n > 0 rule.
No. Negative integers fail n >= 0.
No. Whole numbers have no fractional part. Values like 2.5 are out of scope for an int check.
A single check is O(1). Printing whole numbers from 0 to N is O(N).

Next: Number Combination

Generate combinations of numbers for interview practice.

Number combination tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

6 people found this page helpful