prime

PHP program to check whether a number prime or not

PHP program to check whether a number is prime or not

In this tutorial, we will discuss the concept of the PHP program to check whether a number is prime or not

In this post, we are going to learn how to check whether a number is prime or not, in PHP programming language with Example program.

Check prime or not

Prime number

The number that can be divided by 1 and itself. it is called a prime number

for Example 2,3,5,7,11,13…

PHP example to check whether a Number is Prime or Not

Check whether the number is prime or not  – method 1

In this program, The user declares and initializes variables as positive numbers, then it will check whether the given number is a prime number or not,  using the PHP language.

Program 1

<?php
$num=10;
$count=0;
for($i=1; $i<=$num; $i++)
{
if(($num % $i)==0)
{
$count++;
}
}
if($count<3)
{
echo "$num is a prime number";
}
else{
echo "$num is not a prime number";
}
?>

When the above code is executed, it produces the following result

10 is not a prime number

 

Check whether the number is prime or not  – method 2

In this program, The user declares and initializes variables as parameters in the PHP function. When calling the function with an argument then it will check whether the given number is a prime number or not using the PHP function.

Program 2

<?php
function checkPrime($num){
$n=0;
for($i=2; $i<($num/2+1); $i++){
  if($num%$i==0){
  $n++;
  break;
  }
}

if($n==0){
echo $num." is a prime number.";
}else{
echo $num." is not a prime number.";
}
}
checkPrime(11);
?>

When the above code is executed, it produces the following result

11 is a prime number.

 

Check whether the number is prime or not  using a function– method 3

In this program, The user declares and initializes variables as parameters in the PHP function. When calling the function with an argument then it will check whether the given number is a prime number or not using the PHP function.

Program 3

<?php
function checkPrime($num){
for($i=2; $i<$num; $i++){
  if($num%$i==0){
  return 0;
  }
}
return 1;
}
$x=checkPrime(27);
if($x==0){
echo "this is not a prime number.";
}else{
echo "this is a prime number.";
}

?>

When the above code is executed, it produces the following result

this is not a prime number.

 

Similar post

Program to find whether a Number is Prime or Not in C++

Program to find whether a Number is Prime or Not in C

Program to find whether a Number is Prime or Not in Java

Program to find whether a Number is Prime or Not in Python

 

C++ program to find first n prime numbers

Java program to find first n prime numbers

C program to find first n prime numbers

 

C code to 5 ways to check whether the given integer is Even or Odd

Java code to 5 ways to check whether the given integer is Even or Odd

C++ code to 5 ways to check whether the given integer is Even or Odd

Python code to 5 ways to check whether the given integer is Even or Odd

 

C# function to check a number is prime or not
PHP function to check whether a number is prime or not
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago