Check Natural Number in C
What you’ll learn
- A down-to-earth meaning of natural number (counting numbers) and the test
n > 0on integers. - A tiny helper
is_naturalplusint main(void)with a clearprintfmessage. - 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, printf, and integer types.
#include <stdio.h>and writingint main(void).- 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.
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 scanf when you wire input.
Test
If num > 0, answer yes; otherwise no.
Print a sentence a human can read.
📜 Pseudocode
function is_natural(num): // integer
if num > 0:
return true
return false Yes / no for one integer
Same structure as the reference: helper returns 1 or 0, int main(void), and sample value 42.
#include <stdio.h>
int is_natural(int num) {
return (num > 0) ? 1 : 0;
}
int main(void) {
int number = 42;
if (is_natural(number)) {
printf("%d is a natural number.\n", number);
} else {
printf("%d is not a natural number.\n", number);
}
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.
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.
#include <stdio.h>
void print_integers_in_range(int start, int end) {
printf("Natural numbers in the range %d to %d:\n", start, end);
for (int i = start; i <= end; ++i) {
printf("%d ", i);
}
printf("\n");
}
int main(void) {
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 scanf, check the return value and reject non-numeric junk before calling is_natural.
❓ FAQ
🔄 Input / output
Both programs use fixed values to keep output predictable. Swap in scanf when you want the user to type the test value or the ends of a range.
Edge cases
num == 0
Not natural under n > 0; may be natural if your syllabus includes 0—adjust the test and wording together.
Very large int
Still an integer; the comparison works. If you need bigger values, consider long long.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
| Single test | O(1) | O(1) |
Print range start..end | O(end - start + 1) | O(1) |
Summary
- Definition used: integer
nwithn > 0. - Patterns: one-off check plus optional range listing.
- Remember: align code with the definition your exam uses (especially for 0).
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.
8 people found this page helpful
