C Whole Number Program

Beginner
5 min read
Updated: Nov 2026
3 programs

What Is a Whole Number?

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
ValueWhole?Why
0YesSmallest whole number
7YesNon-negative integer
-3NoNegative
3.5NoHas a fractional part

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.

  1. Read the value (int for integer input, double when decimals are possible).
  2. For int, test n >= 0.
  3. For double, test isfinite(x) && x >= 0 && x == floor(x).
  4. Print whether the value is a whole number.
  5. Compile with gcc file.c -std=c11 -o out -lm when you use floor.
Input typeTestHeader
intn >= 0stdio.h
doublex >= 0 && x == floor(x)math.h
Unsigned typesAlways 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

Cheat Sheet

Quick facts for the whole-number drill.

TopicDetail
Set{0, 1, 2, 3, ...}
Smallest0
Versus naturalNatural starts at 1; whole includes 0
Integer testn >= 0
Decimal testx >= 0 && x == floor(x)
Special doublesReject NaN and infinity with isfinite

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)

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;
      }

How It Works

  1. Read an int. Fractions cannot occur, so only the sign matters.
  2. Test n >= 0. Zero passes; natural-number checks would reject it.
  3. 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;
      }

How It Works

  1. Read a double. The user may type a fraction.
  2. Reject bad values. isfinite rules out NaN and infinity.
  3. Compare with floor. A whole value equals its own floor.
  4. 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;
      }

How It Works

  1. Start at 0. That is the first whole number.
  2. Stop at n. i <= n includes 5.
  3. 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.

Performance Notes

CheckCostNotes
int sign testO(1)One comparison
double floor testO(1)One call plus comparisons
Print 0 to nO(n)One pass

All checks are constant time. The only cost is reading input and printing results.

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.
Yes. Whole numbers are the non-negative integers.

Next: Number Combination

Count combinations with nCr.

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.

8 people found this page helpful