September 22, 2021, Learn eTutorial

851

**Prime numbers** are natural numbers that are greater than **1** that are not a product of two smaller natural numbers. Prime numbers are natural numbers that are greater than **1** that are not a product of two smaller natural numbers.

**Composite numbers** are natural numbers greater than **1** and which are not prime numbers. For example, **7** has no divisors other than **1** and **7** itself which is a prime number and **6 ** has more than two divisors which is a composite number.

For example, 2, 3, 5, 7, 11, 13, etc are prime numbers because they have only 2 divisors and the number '4', '6', which have more than two divisors are composite numbers.

Let us take an example as the number '5' is a prime number because the number '5' has only two divisors: '1' and '5'. So the prime number has two divisors only. But, 4 is not prime (it is composite) since 2 x 2 = 4.

In PHP to check whether the number is **Prime** by using **Mod(%)** Operator and if we get 0 except by 1 and the number itself, it won't be a prime number. In this program, we first have to accept the limit from the user and assign it to the variable **n **and assign the value **0** into the variable **ct** and value **2** into the variable **num. **Then perform the `while`

loop until the condition **'ct < n' ** becomes false and in the block of the loop we have to assign the value **0** into the variable** dCt. **And then assign the value **2** into the variable **i** perform the `for`

loop until the condition **'i <= num'** becomes false and increment the value of the **i** in every iteration in the block of the loop we have to check the condition **'num % i == 0'** if tru increment the value of the variable **dCt. **And after the completion of the `for`

loop, we have to check the condition **'dCt == 1'** if true print the value of the variable **num** and also increment the value of the variable **ct **and increment the value of the variable **num.**

**Step 1: **Accept the limit from the user and assign it to the variable **n**

**Step 2:** Assign the value **0** into the variable **ct** and value **2** into the variable **num**

**Step 3:** Perform the following Steps until the condition **'ct < n' ** becomes false

**Step 4:** Assign the value **0** into the variable** dCt**

**Step 5:** Assign the value **2** into the variable **i** perform the following sub-step until the condition **'i <= num'** becomes false and increment the value of the **i** in every iteration

(i) Check the condition **'num % i == 0'** if tru increment the value of the variable **dCt**

**Step 6:** Check the condition **'dCt == 1'** if true print the value of the variable **num** and also increment the value of the variable **ct**

**Step 7:** Increment the value of the variable **num**

` ````
<?php
$n = readline("Enter the limit of number required: ");
$ct = 0;
$num = 2;
while ($ct < $n) {
$dCt = 0;
for ($i = 2; $i <= $num; $i++) {
if (($num % $i) == 0) {
$dCt++;
}
}
if ($dCt == 1) {
echo $num . " ";
$ct++;
}
$num++;
}
?>
```

Enter the limit of number required: 15 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47