Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C Program to check Natural Number

Posted in C Tutorial
Updated on Oct 06, 2024
By Mari Selvan
πŸ‘οΈ 653 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
C Program to check Natural Number

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the realm of programming, understanding and validating numbers is a fundamental task. One common classification is that of natural numbers.

A natural number is a positive integer greater than zero.

In this tutorial, we will explore a simple C program that checks whether a given number is a natural number or not.

πŸ“„ Example

Let's delve into the C code that accomplishes this functionality.

isNaturalNumber.c
Copied
Copy To Clipboard
#include <stdio.h>

// Function to check if a number is a natural number
int isNaturalNumber(int num) {
  return (num > 0) ? 1 : 0;
}

// Driver program
int main() {
  // Replace this value with your desired number
  int number = 42;

  // Check if the number is a natural number
  if (isNaturalNumber(number)) {
    printf("%d is a natural number.\n", number);
  } else {
    printf("%d is not a natural number.\n", number);
  }

  return 0;
}

πŸ’» Testing the Program

To test the program with different numbers, replace the value of number in the main function.

Output
42 is a natural number.

Compile and run the program to see if the given number is a natural number.

🧠 How the Program Works

  1. The program defines a function isNaturalNumber that takes a number as input and returns 1 if it's a natural number (greater than 0) and 0 otherwise.
  2. In the main function, replace the value of number with the desired number.
  3. The program then checks if the number is a natural number using the isNaturalNumber function.
  4. It prints whether the number is a natural number or not based on the result.

πŸ€” Considerations

  • The program assumes the input number is an integer.
  • For user input scenarios, additional validation may be required to handle non-integer inputs.

πŸ“ Between the Given Range

Let's dive into the C code that checks and displays natural numbers in the range from 1 to 10.

checkNaturalNumbers.c
Copied
Copy To Clipboard
#include <stdio.h>

// Function to check and display natural numbers in a range
void checkNaturalNumbers(int start, int end) {
    printf("Natural Numbers in the range %d to %d:\n", start, end);

    // Iterate through the range
    for (int i = start; i <= end; ++i) {
        printf("%d ", i);
    }

    printf("\n");
}

// Driver program
int main() {
    // Define the range
    int start = 1;
    int end = 10;

    // Call the function to check and display natural numbers
    checkNaturalNumbers(start, end);

    return 0;
}

πŸ’» Testing the Program

The program is set to display natural numbers in the range from 1 to 10. You can customize the range by modifying the values of start and end in the main function.

Output
Natural Numbers in the range 1 to 10:
1 2 3 4 5 6 7 8 9 10 

Compile and run the program to see the natural numbers in the specified range.

🧠 How the Program Works

  1. The program defines a function checkNaturalNumbers that takes the start and end values of the range and prints the natural numbers in that range.
  2. Inside the function, it uses a for loop to iterate through the specified range and prints each natural number.
  3. The main function sets the range from 1 to 10 and calls the function to display the natural numbers in that range.

🧐 Understanding the Concept of Natural Numbers

Natural numbers are a set of positive integers that start from 1 and continue indefinitely. They are often used for counting and ordering.

In this program, we check whether a given number is a natural number by verifying if it is greater than zero.

πŸŽ‰ Conclusion

This C program serves as a foundational example for checking whether a number is a natural number or not. Understanding the characteristics of natural numbers and incorporating such checks can be essential in various programming scenarios

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
11 months ago

If you have any doubts regarding this article (C Program to check Natural Number), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy