Write a php program to print all prime numbers between 1 to n

What is a Prime Number & Composite Number?

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   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.

How to n prime numbers using PHP?

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 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

Chủ Đề