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:

<?php

// Function to check if a number is prime
function isPrime($num) {
  // Check if the number is less than 2, which is not prime
  if ($num < 2) {
    return false;
  }

  // Check if the number is divisible by any integer from 2 to the square root of the number
  for ($i = 2; $i <= sqrt($num); $i++) {
    if ($num % $i == 0) {
      return false;
    }
  }

  // If the number is not divisible by any integer from 2 to the square root of the number, it is prime
  return true;
}

// Read a number from the user
echo "Enter a number: ";
$num = trim(fgets(STDIN));

// Check if the number is prime
if (isPrime($num)) {
  echo "$num is a prime number.\n";
} else {
  echo "$num is not a prime number.\n";
}

?>

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:

<?php

// Function to check if a number is prime
function isPrime($num) {
  // Check if the number is less than 2, which is not prime
  if ($num < 2) {
    return false;
  }

  // Initialize the divisor to 2
  $divisor = 2;

  // Loop until the divisor is greater than the square root of the number
  while ($divisor <= sqrt($num)) {
    // If the number is divisible by the divisor, it is not prime
    if ($num % $divisor == 0) {
      return false;
    }

    // Increment the divisor
    $divisor++;
  }

  // If the number is not divisible by any integer from 2 to the square root of the number, it is prime
  return true;
}

// Read a number from the user
echo "Enter a number: ";
$num = trim(fgets(STDIN));

// Check if the number is prime
if (isPrime($num)) {
  echo "$num is a prime number.\n";
} else {
  echo "$num is not a prime number.\n";
}

?>

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