Java Basic
Java Interview Programs
- Java Interview Programs
- Java Abundant Number
- Java Amicable Number
- Java Armstrong Number
- Java Average of N Numbers
- Java Automorphic Number
- Java Biggest of three numbers
- Java Binary to Decimal
- Java Common Divisors
- Java Composite Number
- Java Condense a Number
- Java Cube Number
- Java Decimal to Binary
- Java Decimal to Octal
- Java Disarium Number
- Java Even Number
- Java Evil Number
- Java Factorial of a Number
- Java Fibonacci Series
- Java GCD
- Java Happy Number
- Java Harshad Number
- Java LCM
- Java Leap Year
- Java Magic Number
- Java Matrix Addition
- Java Matrix Division
- Java Matrix Multiplication
- Java Matrix Subtraction
- JS Matrix Transpose
- Java Maximum Value of an Array
- Java Minimum Value of an Array
- Java Multiplication Table
- Java Natural Number
- Java Number Combination
- Java Odd Number
- Java Palindrome Number
- Java Pascalβs Triangle
- Java Perfect Number
- Java Perfect Square
- Java Power of 2
- Java Power of 3
- Java Pronic Number
- Java Prime Factor
- Java Prime Number
- Java Smith Number
- Java Strong Number
- Java Sum of Array
- Java Sum of Digits
- Java Swap Two Numbers
- Java Triangular Number
Java 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 straightforward Java program that checks whether a given number is a natural number or not.
π Example
Let's dive into the Java code that accomplishes this functionality.
public class CheckNaturalNumber {
// Function to check if a number is a natural number
static boolean isNaturalNumber(int num) {
return num > 0;
}
// Driver program
public static void main(String[] args) {
// Replace this value with your desired number
int number = 42;
// Check if the number is a natural number
if (isNaturalNumber(number)) {
System.out.println(number + " is a natural number.");
} else {
System.out.println(number + " is not a natural number.");
}
}
}
π» Testing the Program
To test the program with different numbers, replace the value of number in the main method.
42 is a natural number.
Compile and run the program to see if the given number is a natural number.
π§ How the Program Works
- The program defines a class CheckNaturalNumber containing a static method isNaturalNumber that takes a number as input and returns true if it's a natural number (greater than 0) and false otherwise.
- In the main method, replace the value of number with the desired number.
- The program then checks if the number is a natural number using the isNaturalNumber method.
- 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 java code that checks and displays natural numbers in the range from 1 to 10.
public class NaturalNumberChecker {
// Function to check and list natural numbers in the range 1 to 10
static void checkAndListNaturalNumbers() {
System.out.print("Natural Numbers in the range 1 to 10: ");
// Iterate through the range
for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
}
System.out.println();
}
// Driver program
public static void main(String[] args) {
// Call the function to check and list natural numbers
checkAndListNaturalNumbers();
}
}
π» Testing the Program
Natural Numbers in the range 1 to 10: 1 2 3 4 5 6 7 8 9 10
To test the program, compile and run it. The output will display the natural numbers in the range 1 to 10.
π§ How the Program Works
- The program defines a class NaturalNumberChecker containing a static method checkAndListNaturalNumbers that prints natural numbers in the range 1 to 10.
- Inside the method, it uses a for loop to iterate through the specified range and prints each natural number.
- The main method calls the function to check and list natural numbers.
π§ 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 java 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:
Author
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
If you have any doubts regarding this article (Java Program to check Natural Number), please comment here. I will help you immediately.