Print prime numbers from 1 to 100 in PHP using for loop and while loop.
In this article, you will learn how to print prime numbers from 1 to 100 in PHP using for loop and while loop.
Example
------The prime numbers from 1 to 60 are------
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59
You should have the knowledge of the following topics in PHP programming to understand this program:
- PHP For loop
- PHP While loop
Sample of Prime Numbers
2 3 5 7 .......... 47 53 59
In this article, we solve this problem using two methods:
- Using the for loop
- Using the while loop
Source Code
Run Program
Output
------The prime numbers from 1 to 60 are------
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59
Explanation
In this given program, we have taken input in the variable $x = 60
to generate the prime numbers up to 60.
Then we applied simple calculation via for loop to check every number of range between 1-60 to find the prime numbers.
If any number is divided by only 1 & itself and which number is divisible by any numbers it means these type numbers are called the prime numbers.
After the whole calculation, this will return these numbers: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59
, It's the final output of the above program.
Print Prime Numbers from 1 to 100 in PHP using While loop
Run Program
This blog describes how to find prime numbers between 1 and 100 using PHP. Prime number is a number which is divided by 1 and itself. Consider the number 5. It is prime number because it can be divided by 1 and 5.
PHP program for prime numbers:
Consider the below program which is used to find prime numbers from 1 to 100.
In above program, if a number is divided by any number except 1 and itself, then it is not prime number. Otherwise it is a prime number.
Now you got prime numbers between 1 and 100 using PHP.
Related Post