prime-number-program-in-php-mymixindia.com

Prime Number Program in PHP


Prime Number Program in PHP 2022

Today we are making a primer number program in PHP by using functions.

<?php
//......Mymixindia.com......
//.....prime number program - check if a given number is a prime number or not using function....

function NumberIsPrime($number) //....funtion for checking prime number
{
    if ($number <= 1)
        return false;

    // Check from 2 to n-1
    for ($i = 2; $i < $number; $i++)
        if ($number % $i == 0)
            return false;

    return true;
}

//....calling the function in if condition.....
if(NumberIsPrime(17))
    echo("Prime Number");
else
    echo("Not a Prime Number");

?>

The output of the program

Prime Number

Explanation of the Program:

  1. Making a function for checking the parameter passing is a prime number or not
  2. If-condition for checking that the given number is less than 1 or equal to 1
  3. if the given number is less than 1 or equal to 1 then it returns false
  4. and then move to the for loop statement where the number is divided by the iteration number and compared with zero for leaving no reminder
  5. on condition true return false else return true
  6. and after that calling the prime number function in if-condition with an argument passing
  7. if the condition is true then the number is a prime number else it is not a prime number

You can learn more about PHP and go through the PHP tutorials.

Related Posts:

Program to Print Prime Number in CPP 2020

5 Namespace Program in C++ 2022

Constructors Program in Java

Java Data Type Program

HTML Tutorials 

CSS tutorials