Some Logical Questions in Interview

 Q 1. print even And odd number from given array.


$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];


$even = [];

$odd = [];

foreach($numbers as $a){

    if($a%2 == 0){

        $even[] = $a;

    }

    else{

        $odd[] = $a;

    }

}

print_r(array('even'=> $even, 'odd'=> $odd));



Q 2. Calculate factorial of any number.


$num = 5;

$a=1;

for($i=1; $i <= $num; $i++){

    $a=$a*$i;

}

echo $a;       //output: 120



Q 3. Swap the variable values without using third variable.


$a =5   //swap $a= 1

$b =1   //swap $b= 5


$a = $a + $b;

$b = $a - $b;

$a = $a - $b;


echo "value of a: " . $a . ". and value of b: " . $b;


Post a Comment

0 Comments

Visual Studio