For ststement

PHP| Check if a number is prime or not

PHP| Check if a number is prime or not

In this article, we will discuss the concept of the PHP| Check if a number is prime or not

In this post, we are going to learn how to write a program to check whether the given number is prime or not and display the result on the screen in the PHP programming language.

Code to check prime number

Check the prime number in PHP – #1

In this program, the user initiates the values to variables “num” as 11 and “count” as 0, and the program checks whether the given number(11) is prime or not in the PHP programming language.

Program 1

<?php
//php code to check whether a number is prime or not
$num=11;
$count=0;
for($i=1; $i<=$num; $i++)
{
if(($num % $i)==0)
{
$count++;
}
}
if($count<3)
{
echo "$num is a prime number";
}//print if the number is prime
else{
echo "$num is not a prime number";
}//print if the number is not prime
?>

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

11 is a prime number

 

Check the prime number in PHP – #2

In this program, the user is asked to enter a number using a form in PHP. and the program checks whether the given number is prime or not in the PHP programming language.

Program 2

<form method="post">
Enter a number: <input type="text" name="input"><br>
<br>
<input type = "submit" name="submit" value="Check"/><br>
</form>
<?php
if($_POST)
{
  $input=$_POST['input'];
  for($i=2; $i<=$input-1; $i++){
    if($input%$i==0){
      $value=True;
    }
  }
  if(isset($value)&&($value)){
    echo 'The Number '.$input.' is not a prime';
  }else{
    echo 'The Number '.$input.' is a prime';
  }
  }
  ?>

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

prime check

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

C# programming code to check prime or not

C# programming code to check prime or not using function

 

Code to print prime numbers from 1 to 100 or 1 to n in Java

Code to print prime numbers from 1 to 100 or 1 to n in C

Code to print prime numbers from 1 to 100 or 1 to n in C++

Code to print prime numbers from 1 to 100 or 1 to n in Python

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.