How to loop numbers from 2 to 100 and print only prime numbers?

php

#1

How to loop numbers from 2 to 100 and print only prime numbers?


#2

to do this you need to first define a function that will be able to detect a prime number i will give that to you here

<?php
	function isprime($num){
		$n = (int) sqrt($num)+1;
		for($i = 2; $i < $n; $i++) {
    		if (($num % $i)==0) {return false;}
        }
    	return true;
	}
?>

Now you can create your loop like this

<?php
	for($i = 2; $i <200; $i++){
		echo isprime($i)?$i."<br>":null;
	}
?>

or

<?php
	for($i = 2; $i <200; $i++){
		if(isprime($i){
			echo $i."<br>"
		}
	}
?>