Write a Prime number checker program using PHP

Hello programmers, Today we are going to create prime number checker program with the help of PHP. So Here is the simple PHP code that checks whether a given number is prime or not:

01<?php
02 
03// Function to check if a number is prime
04function isPrime($num) {
05  // Check if the number is less than 2, which is not prime
06  if ($num < 2) {
07    return false;
08  }
09 
10  // Check if the number is divisible by any integer from 2 to the square root of the number
11  for ($i = 2; $i <= sqrt($num); $i++) {
12    if ($num % $i == 0) {
13      return false;
14    }
15  }
16 
17  // If the number is not divisible by any integer from 2 to the square root of the number, it is prime
18  return true;
19}
20 
21// Read a number from the user
22echo "Enter a number: ";
23$num = trim(fgets(STDIN));
24 
25// Check if the number is prime
26if (isPrime($num)) {
27  echo "$num is a prime number.\n";
28} else {
29  echo "$num is not a prime number.\n";
30}
31 
32?>

This program defines a function isPrime() that takes a number as an argument and returns true if the number is prime, or false if it is not. The function first checks if the number is less than 2, in which case it is not prime. Then, it checks if the number is divisible by any integer from 2 to the square root of the number. If the number is not divisible by any of these integers, it is considered prime.

The program then prompts the user to enter a number, reads the input, and calls the isPrime() function to check if the number is prime. Finally, it displays a message indicating whether the number is prime or not.

Here’s an example of a PHP program that uses a while loop to check whether a given number is prime:

01<?php
02 
03// Function to check if a number is prime
04function isPrime($num) {
05  // Check if the number is less than 2, which is not prime
06  if ($num < 2) {
07    return false;
08  }
09 
10  // Initialize the divisor to 2
11  $divisor = 2;
12 
13  // Loop until the divisor is greater than the square root of the number
14  while ($divisor <= sqrt($num)) {
15    // If the number is divisible by the divisor, it is not prime
16    if ($num % $divisor == 0) {
17      return false;
18    }
19 
20    // Increment the divisor
21    $divisor++;
22  }
23 
24  // If the number is not divisible by any integer from 2 to the square root of the number, it is prime
25  return true;
26}
27 
28// Read a number from the user
29echo "Enter a number: ";
30$num = trim(fgets(STDIN));
31 
32// Check if the number is prime
33if (isPrime($num)) {
34  echo "$num is a prime number.\n";
35} else {
36  echo "$num is not a prime number.\n";
37}
38 
39?>

This program works in a similar way to the previous example, but instead of using a for loop to check if the number is divisible by any integer from 2 to the square root of the number, it uses a while loop. The loop starts with a divisor of 2 and continues until the divisor is greater than the square root of the number, incrementing the divisor at each iteration. If the number is divisible by the divisor at any point, the function returns false to indicate that the number is not prime. If the loop completes without finding a divisor, the function returns true to indicate that the number is prime.

I hope this helps! Let me know if you have any questions.

Leave a Comment