PHP Exercise : PHP Prime Number Program

Write a program in PHP to print prime numbers between 1 and 100.

A prime number is a whole number greater than 1 whose only factors are 1 and itself, like -2, 3, 5, 7, 11 etc. For example, 17 is a prime number because it is only divisible by 1 and 17. On the other hand, 18 is not a prime number because it is divisible by 2, 3, 6, 9 and the number itself. These are different ways to print prime number between 1 to 100 in the PHP programming language.

Solution 1: Prime Number Program using While Loop

The following code prints a list of prime numbers between 1 and 100 (that is, numbers not divisible by something other than 1 or the number itself) using a while loop.

<?php  
	$limit = 100;
	$init = 2;
	
	while(TRUE)
	{
		$div = 2;
		if($init > $limit) 
		{
			break;
		}
		while(TRUE)
		{
			if($div > sqrt($init))
			{
				echo $init."  ";
				break;
			}
			if($init % $div == 0) 
			{
				break;
			}
			$div = $div + 1;
		}
		$init = $init + 1;
	}
?>

Output of the above code

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

In the above code, we have two while loops. The inner while loop does the testing with each possible divisor. If the inner loop finds a divisor, the number is not prime, so it breaks out without printing anything, and if the testing gets as high as the square root of the number, we can assume that the number is prime. The outer loop works through all the numbers between 1 and 100. This loop is broken when we have reached the breaking point of numbers to test.





Solution 2 : Prime Number Program using For Loop

Here, we have created a function 'checkPrime()' to check whether the number is prime or not. We loop over the number range (1 to 100) and pass each as a parameter to the function to check whether a number is prime or not.

<?php
function checkPrime($num)
{
   if ($num == 1)
   return 0;
   for ($i = 2; $i <= $num/2; $i++)
   {
      if ($num % $i == 0)
      return 0;
   }
   return 1;
}

echo '<h2>Prime Numbers between 1 and 100</h2> ';
for($num = 1; $num <= 100; $num++) {
	$flag = checkPrime($num);
	if ($flag == 1) {
		echo $num." ";
	}	
}  
?>

Output of the above code:

Prime Numbers between 1 and 100 :

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




Related PHP Programs for Practice

PHP - Division table program
PHP - Prime Number Program
PHP - Print numbers 10 to 1 using recursion
PHP - Loop through an associative array
PHP - Differentiate between fgets, fgetss and fgetcsv
PHP - Set session on login
PHP - Convert text to speech
PHP - Copying or moving a file
PHP - Locking a file
PHP - CURL Cookie Jar
PHP - Password Hashing
PHP - Emoji Unicode Characters
PHP - Calculate percentage of total
PHP - Sanitize input for MySQL
PHP - Random quote generator
PHP - Password Hashing
PHP - Emoji Unicode Characters
PHP - Age calculator
PHP - Get array key
PHP - Capitalize first letter
PHP - Set Timezone
PHP - Remove duplicates from array
PHP - Fibonacci Series Program
PHP - Lock a file
PHP - Insert image in database




Read more articles


General Knowledge



Learn Popular Language