In this post, you will learn about programming for prime numbers and get the prime numbers between two number ranges.
A prime number is a whole number greater than 1 whose only factors are 1 and itself
Let us find the prime number between 1- 100 using the PHP script. Here, we have created a function ‘IsPrime()’ to check whether the number is prime or not. now we need to call the function passing the value from 1 – 100 by applying a loop in PHP.
Example-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php function IsPrime($num){ if ($num == 1) return 0; for ($i = 2; $i <= $num/2; $i++) { if ($num % $i == 0) return 0; } return 1; } for($num = 1; $num <= 100; $num++) { $flag = IsPrime($num); if ($flag == 1) { echo $num." "; } } ?> |
Output:
1 2 | 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 |