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

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

1. Using the for loop
2. Using the while loop

Source Code

``````<?php
// Print Prime Numbers from 1 to 100 in PHP using For loop

\$x = 60;
echo "------The prime numbers from 1 to ".\$x." are------\n\n";

for (\$i = 0; \$i < \$x; \$i++) {

// There are niether prime nor composite if as skip 0 and 1 number
if (\$i == 1 || \$i == 0)
continue;

\$f = 1;

for (\$j = 2; \$j < intval(\$i / 2) + 1; \$j++) {
if (\$i % \$j == 0) {
\$f = 0;
break;
}
}

// If f = 1 means i is prime number and f = 0 means i is not prime number
if (\$f == 1)
echo \$i." ";
}
echo "\n";

?>``````

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.

``````<?php
// Print Prime Numbers from 1 to 100 in PHP using While loop

\$x = 60;
echo "------The prime numbers from 1 to ".\$x." are------\n\n";

\$i = 0;
while (\$i < \$x) {

// There are niether prime nor composite if as skip 0 and 1 number
if (\$i == 1 || \$i == 0) {
\$i++;
continue;
}

\$f = 1;

\$j = 2;
while (\$j < intval(\$i / 2) + 1) {
if (\$i % \$j == 0) {
\$f = 0;
break;
}
\$j++;
}

// If f = 1 means i is prime number and f = 0 means i is not prime number
if (\$f == 1)
echo \$i." ";

\$i++;
}
echo "\n";

?>``````

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.

<?php
for(\$i = 1; \$i <= 100; \$i++)
\$mm = 0;
for(\$j = 2; \$j <= \$i/2; \$j++) {
//only not prime numbers
if (\$i % \$j == 0) {
\$mm++;
break;
}
}
if (\$mm == 0) {
echo"\$i is prime number<br/>";
}
}
?>

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

How do you find the prime number between 1 and 100?

How many prime numbers are there from 1 to 100? There are 25 prime numbers between 1 to 100 which are 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.

What are prime numbers in php?

A number which is only divisible by 1 and itself is called prime number. Numbers 2, 3, 5, 7, 11, 13, 17, etc. are prime numbers.

How do you print prime numbers from 1 to 50?

Print Prime Numbers from 1 to 50.
Take a variable say count and initialize it with 0 at beginning of the program..
Create a for loop and start it from 1 to 50..
Inside the for loop, create another for loop with different loop variable say j..

What is the sum of all prime numbers from 1 to 100?

So the sum of all the good prime numbers between 1 and 100 is 29+89=118 29 + 89 = 118 .

Tải thêm tài liệu liên quan đến bài viết Write a program in php to print prime numbers between 1 and 100