A whole number is a non-negative integer: 0, 1, 2, 3, ... with no fractions and no negatives. It is the set of natural numbers plus 0.
Remember
whole numbers: 0, 1, 2, 3, 4, ...
natural numbers: 1, 2, 3, 4, ... (no 0)
integers: ..., -2, -1, 0, 1, 2, ...
n is whole <=> n >= 0 and n has no fractional part
Value
Whole?
Why
0
Yes
Smallest whole number
7
Yes
Non-negative integer
-3
No
Negative
3.5
No
Has a fractional part
Approach
How to Check a Whole Number in C
For an int the test is a sign check. For a double you also need to confirm there is no fractional part.
Read the value (int for integer input, double when decimals are possible).
For int, test n >= 0.
For double, test isfinite(x) && x >= 0 && x == floor(x).
Print whether the value is a whole number.
Compile with gcc file.c -std=c11 -o out -lm when you use floor.
Input type
Test
Header
int
n >= 0
stdio.h
double
x >= 0 && x == floor(x)
math.h
Unsigned types
Always whole
—
Pseudocode
Pseudocode
read value
if value is an int:
whole = (value >= 0)
else if value is a double:
whole = finite AND value >= 0 AND value == floor(value)
print whole or not whole
Reference
Cheat Sheet
Quick facts for the whole-number drill.
Topic
Detail
Set
{0, 1, 2, 3, ...}
Smallest
0
Versus natural
Natural starts at 1; whole includes 0
Integer test
n >= 0
Decimal test
x >= 0 && x == floor(x)
Special doubles
Reject NaN and infinity with isfinite
Trace
Worked Walkthrough — Three Inputs
Run the double test on 5, -2, and 3.5.
Calculation
x = 5 finite, 5 >= 0, floor(5) = 5 -> whole
x = -2 -2 >= 0 is false -> not whole
x = 3.5 3.5 >= 0, floor(3.5) = 3 != 3.5 -> not whole
For int n = 0: n >= 0 is true -> whole (natural numbers would say no)
Code
C Examples
Compile with gcc file.c -std=c11 -o out -lm. Compare with Natural Number, which excludes 0.
Example 1 — Check an Integer
A sign test is enough when the input is already an integer.
C
#include <stdio.h>
int main(void) {
int n;
printf("Enter an integer: ");
if (scanf("%d", &n) != 1) {
printf("Invalid input\n");
return 1;
}
if (n >= 0) {
printf("%d is a whole number\n", n);
} else {
printf("%d is not a whole number\n", n);
}
return 0;
}
Output
Enter an integer: 0
0 is a whole number
How It Works
Read an int. Fractions cannot occur, so only the sign matters.
Test n >= 0. Zero passes; natural-number checks would reject it.
Print the verdict. Negative values fail.
Example 2 — Check a Decimal Input
For floating-point input, also require that the value has no fractional part.
C
#include <math.h>
#include <stdio.h>
int main(void) {
double x;
printf("Enter a number: ");
if (scanf("%lf", &x) != 1) {
printf("Invalid input\n");
return 1;
}
if (isfinite(x) && x >= 0.0 && x == floor(x)) {
printf("%g is a whole number\n", x);
} else {
printf("%g is not a whole number\n", x);
}
return 0;
}
Output
Enter a number: 3.5
3.5 is not a whole number
How It Works
Read a double. The user may type a fraction.
Reject bad values.isfinite rules out NaN and infinity.
Compare with floor. A whole value equals its own floor.
Result for 3.5.floor(3.5) is 3, so it is not whole.
Example 3 — Print Whole Numbers from 0 to 5
Starting the loop at 0, not 1, is what separates whole numbers from natural numbers.
C
#include <stdio.h>
int main(void) {
int n = 5;
int sum = 0;
for (int i = 0; i <= n; i++) {
printf("%d ", i);
sum += i;
}
printf("\nsum = %d\n", sum);
return 0;
}
Output
0 1 2 3 4 5
sum = 15
How It Works
Start at 0. That is the first whole number.
Stop at n.i <= n includes 5.
Sum. 0 + 1 + 2 + 3 + 4 + 5 = 15.
Edge Cases & Pitfalls
Check these before calling a value whole.
zero
Zero belongs here
Natural-number programs usually reject 0. A whole-number program must accept it.
float
Floating-point error
Values like 0.1 * 3 may not equal their floor exactly. Compare with a tolerance if the input comes from calculations.
special
NaN and infinity
floor(INFINITY) == INFINITY is true, so check isfinite first.
link
Linking math
floor needs -lm on many GCC setups.
Analysis
Performance Notes
Check
Cost
Notes
int sign test
O(1)
One comparison
double floor test
O(1)
One call plus comparisons
Print 0 to n
O(n)
One pass
All checks are constant time. The only cost is reading input and printing results.
Remember
Key Takeaways
Definition: whole numbers are 0, 1, 2, 3, and so on.
Versus natural: whole = natural numbers plus 0.
Int test:n >= 0.
Double test: non-negative, finite, and equal to its floor.
One line: a whole number is a non-negative integer, and 0 counts.
Frequently Asked Questions
A whole number is a non-negative integer: 0, 1, 2, 3, and so on. It has no fractional or negative part.
Natural numbers start at 1 in most school definitions, while whole numbers start at 0.
Yes. 0 is the smallest whole number.
Use isfinite(x) && x >= 0 && x == floor(x). Include math.h and link with -lm if needed.
No. Whole numbers are 0 and above. Negative integers are integers, not whole numbers.