Programmings Based On Array And String Functions To Find How Many Numbers Of Positive, Negative And Zero Numbers Present In The Array $arr=array(-1,-2,-3,-4,0,0,1,2,3,4)

Programmings Based On Array And String Functions To Find How Many Numbers Of Positive, Negative And Zero Numbers Present In The Array $arr=array(-1,-2,-3,-4,0,0,1,2,3,4)

Q1. Find how many numbers of Positive, Negative, 0 numbers present in this array.

$arr=array(-1,-2,-3,-4,0,0,1,2,3,4)

Ans: Coding =>

<?php
$arr=array(-1,-2,-3,-4,0,0,1,2,3,4);
$pn=0;$nn=0;$zn=0;
$len=count($arr);
for ($i=0; $i < $len; $i++) {
    if ($arr[$i]>0) {
        $pn++;
    }
    else if ($arr[$i]<0) {
        $nn++;
    }
    else{
        $zn++;
    }
}
echo "The positive number present in the array is: ".$pn."<br>";
echo "The negative number present in the array is: ".$nn."<br>";
echo "The zero number present in the array is: ".$zn;
?>

Output Of The Coding =>


Explanation Of The Coding =>

In $arr=array(-1,-2,-3,-4,0,0,1,2,3,4), -1 is in the 0 position, -2 is in the 1st position, -3 is in the 2nd position, -4 is in the 3rd position, 0 is in the 4th position, 0 is in the 5th position, 1 is in the 6th position, 2 is in the 7th position, 3 is in the 8th position and 4 is in the 9th position.

Then $len=count($arr) where $len=count(array(-1)) i.e. $len=count(0) where for($i=0;0<-1;$i++) and as 0 is not less than -1 so it will go into the elseif part and if in elseif part $arr[0]<0 i.e. if in elseif part -1<0 and as this condition satisfies -1<0 so it will print $nn++ i.e. it will print -1 as the negative number $nn++.

And again now $i will increase by 1 as it is $i++ so now $i will be 0+1 i.e. 1 and $len=count(array(-2)) i.e. $len=count(1) again now it will check the for loop $i=1;1<-2;$i++) and as 1 is not less than -2 so it will go into the elseif part and if in elseif part $arr[1]<0 i.e. if in elseif part -2<0 and as this condition satisfies -2<0 so it will print $nn++ i.e. it will print -2 as the negative number $nn++.

So in this way the loop will continue to rotate.


Comments

Popular posts from this blog

Programmings Based On Array And String Functions To Merge The 2 Arrays, Then After Merge Convert Into String And Then Find Out The 1st And Last Word From The Reverse String

PHP WEB DEVELOPMENT INTERVIEW QUESTIONS ASKED IN APOLLO HOSPITAL ALONG WITH MY OWN CONCEPTS AND MY OWN EXPLANATION ANSWERS(PART - 1) REST OF THE QUESTIONS AND ANSWERS WILL BE PREPARED BY ME IN MY NEXT BLOG

Mini Project On Multi User CRUD Operations(Create, Read, Update & Delete) Of Donar Details Of Indian Nationals Who Donate To Ramkrishna Math, Belur Math Using PDO (PHP Data Objects) With Proper Explanation Of Each And Every Individual Line Of The Code Of connection.php Page, form.php Page, formaction.php Page, admin_display.php Page, client_display.php Page, edit.php Page, editaction.php Page And delet.php Page (Here You Will Find The Image Of Sayang Sri Ramkrishna Dev, Sayang Sri Ma Sarada Devi & Sayang Sri Swami Vivekananda) In My CRUD Operation