Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--Write a PHP script PrimesInRange.php that receives two numbers – start and end – from an input field and displays all numbers in that range as a comma-separated list. Prime numbers should be bolded. Styling the page is optional.-->
- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta charset="UTF-8"/>
- <title>The Primes In Range</title>
- <style type="text/css">
- p {
- font-size: 18px;
- color: #555;
- }
- strong {
- font-weight: 800;
- color: #000;
- }
- </style>
- </head>
- <body>
- <form method="post">
- <p>
- <label for="number-min">Starting Index:</label>
- <input type="number" name="start" id="number-min" min="1" required/>
- <label for="number-max">End:</label>
- <input type="number" name="end" id="number-max" min="2" required/>
- <input type="submit" value="Submit"/>
- </p>
- </form>
- <p>
- <?php
- if (!array_key_exists('start', $_POST) || !array_key_exists('end', $_POST) ||
- (int)$_POST['start'] >= (int)$_POST['end']
- ) {
- die('The Input Data is OUT of Range or Form is Empty!!!');
- }
- $start = (int)$_POST['start'];
- $end = (int)$_POST['end'];
- for ($i = $start; $i <= $end; $i++) {
- $isPrime = true;
- $maxDivider = (int)sqrt($i);
- for ($j = 2; $j <= $maxDivider; $j++) {
- if (($i % $j) === 0) {
- $isPrime = false;
- break;
- }
- }
- if ($isPrime) {
- echo "<strong>$i</strong>";
- } else {
- echo $i;
- }
- if ($i < $end) {
- echo ', ';
- }
- }
- ?>
- </p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement