prime

PHP function to check whether a number is prime or not

PHP function to check whether a number is prime or not

In this tutorial, we will discuss the concept of the PHP function 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, using a user-defined function in PHP programming language with Example programs.

Check whether 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 the prime number using a function– method 1

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

Program 1

<?php
function checkPrime($num){
    //function definition
for($i=2; $i<$num; $i++){
  if($num%$i==0){
  return 0;
  }
}
return 1;
}
$x=checkPrime(29);
//Call the function with argument
if($x==0){
echo "this is not a prime number.";
//print if the number is not prime
}else{
echo "this is a prime number.";
//print if the number is prime
}

?>

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

this is a prime number.

 

Check the prime number using a function– method 2

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

Program 2

<?php
function checkPrime($num){
    //function definition
$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.";
//print if the number is a prime
}else{
echo $num." is not a prime number.";
//print if the number is not prime
}
}
checkPrime(17);
//Call the function with argument
?>

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

17 is 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

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

Program to find whether a Number is Prime or Not using the PHP function

 

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

PHP program to check whether a number prime or not
Write a program for printing first n prime numbers using C#
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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago