PHP Basic
PHP Interview Programs
- PHP Interview Programs
- PHP Abundant Number
- PHP Amicable Number
- PHP Armstrong Number
- PHP Average of N Numbers
- PHP Automorphic Number
- PHP Biggest of three numbers
- PHP Binary to Decimal
- PHP Common Divisors
- PHP Composite Number
- PHP Condense a Number
- PHP Cube Number
- PHP Decimal to Binary
- PHP Decimal to Octal
- PHP Disarium Number
- PHP Even Number
- PHP Evil Number
- PHP Factorial of a Number
- PHP Fibonacci Series
- PHP GCD
- PHP Happy Number
- PHP Harshad Number
- PHP LCM
- PHP Leap Year
- PHP Magic Number
- PHP Matrix Addition
- PHP Matrix Division
- PHP Matrix Multiplication
- PHP Matrix Subtraction
- PHP Matrix Transpose
- PHP Maximum Value of an Array
- PHP Minimum Value of an Array
- PHP Multiplication Table
- PHP Natural Number
- PHP Number Combination
- PHP Odd Number
- PHP Palindrome Number
- PHP Pascalβs Triangle
- PHP Perfect Number
- PHP Perfect Square
- PHP Power of 2
- PHP Power of 3
- PHP Pronic Number
- PHP Prime Factor
- PHP Prime Number
- PHP Smith Number
- PHP Strong Number
- PHP Sum of Array
- PHP Sum of Digits
- PHP Swap Two Numbers
- PHP Triangular Number
PHP 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 PHP program that checks whether a given number is a natural number.
π Example
Let's examine the PHP code that achieves this functionality.
<?php
// Function to check if a number is a natural number
function isNaturalNumber($num)
{
return ($num > 0) ? true : false;
}
// Driver program
// Replace this value with your desired number
$number = 42;
// Check if the number is a natural number
if (isNaturalNumber($number))
{
echo "$number is a natural number.\n";
}
else
{
echo "$number is not a natural number.\n";
}
?>
π» Testing the Program
To test the program with different numbers, replace the value of $number in the script.
42 is a natural number.
Run the script to see if the given number is a natural number.
π§ How the Program Works
- The program defines a function 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 part of the script, replace the value of $number with the desired number.
- The program then checks if the number is a natural number using the isNaturalNumber function.
- 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 php code that checks and displays natural numbers in the range from 1 to 10.
<?php
// Function to check if a number is natural
function isNatural($number)
{
return $number > 0 && is_int($number);
}
// Display natural numbers in the range 1 to 10
echo "Natural Numbers in the Range 1 to 10: ";
for ($i = 1;$i <= 10;$i++)
{
if (isNatural($i))
{
echo $i . " ";
}
}
?>
π» Testing the Program
Natural Numbers in the range 1 to 10: 1 2 3 4 5 6 7 8 9 10
The provided code has a fixed range from 1 to 10. You can run the script to see the natural numbers within this range.
π§ How the Program Works
- The program defines a function isNatural that checks if a number is a natural number by ensuring it is a positive integer.
- The for loop iterates through the numbers from 1 to 10.
- For each iteration, the program checks if the current number is a natural number using the isNatural function.
- If it is, the number is displayed.
π§ 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 php 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 (PHP Program to check Natural Number), please comment here. I will help you immediately.