PHP Online Compiler
Example: Print Prime Numbers from 1 to 100 in PHP using While loop
C
C++
C#
Java
Python
PHP
main.php
STDIN
Run
<?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"; ?>
Output
Clear
ADVERTISEMENTS