55. PHP Program to find Positive numbers, Negative numbers and Zeros of given numbers.
How does this program work?
- In this program we are going to learn about how to find the Positve numbers, Negative numbers and Zeros of given real numbers using PHP.
- Declare a variable, and array elements can be stored in that variable.
- Each element is compared with 0, If the element is greater than 0 then the element is postive and the element is less than 0 then the element is negative otherwise the element is zero.
- After doing this for each element of the given array, then we get the postive, negative and zeros numbers of given numbers.
Here is the code
<html>
<head>
<title>PHP Program To find Positive numbers, Negative numbers and Zeros of given numbers</title>
</head>
<body>
<?php
$a = array (54,-85,0,-5,16,0,48,-14,21,0);
$positive = 0;
$negative = 0;
$zero = 0;
//To print array of elements
foreach( $a as $b )
{
echo $b ." ";
}
foreach($a as $n)
{
//To check if given number is positive, negative or zero
if($n > 0)
$positive++;
else if($n < 0)
$negative++;
else
$zero++;
}
echo " Number of +ve numbers are : ".$positive;
echo "Number of -ve numbers are : ".$negative;
echo "Number of Zeros are : ".$zero;
return 0;
?>
</body>
</html>