John Davidson

php - please help me to resolve a problem with loops

0 comments
Message:


I have to resolve this exercice :
Display all the numbers in descending order between 0 and 100, then display hurray each prime number between 100 and zero within the same loop that displays the decreasing eveners.


I did this code :


for($n=100; $n>0; $n-=1 ){
$tableaunombrepaires[]= $n;
//loop for dividers
for($j=2; $j<=$n/2; $j++) {
$diviseurs[]= $j;
}
// here we devide
foreach($diviseurs as $diviseur){
$restes[]=$n%$diviseur;
}
if(!(in_array(0,$restes))){
echo "$n hourrah! \n";
}
else{
echo "$n \n";
}

}

but it doesn't work
help please



My Answer:
=0; $n--){
if($n % 2 == 0){
echo $n . " ";
}
if(is_prime($n)){
echo "Hurray! " . $n . " is a prime number. ";
}
}

function is_prime($num){
if($num < 2){
return false;
}
for($i=2; $i<=sqrt($num); $i++){
if($num % $i == 0){
return false;
}
}
return true;
}

This code will first display all the even numbers in descending order between 0 and 100, and then display "Hurray!" for each prime number between 100 and 0. The is_prime function is used to check if a number is prime or not.

Rate this post

5 of 5 based on 6807 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us