PHP program to check whether the given number is prime or not

This post is very important for IT Students and IT freshers.

The most important program asked in various interviews or technical tests:
Write a PHP program to check whether the given number is prime or not.




What is Prime number: the number which is divisible by 1 and itself only.


So, here is the Program to check whether the given number is prime or not.

<?php
// What is Prime number: the number which is divisible by 1 and itself only.
$n=6; // The number which we want to check if it is PRIME or NOT PRIME
$prime=true;
for($i=2;$i<$n;$i++){
if($n%$i == 0){
$prime = false; // If Number is divisible by the $i iteration, it is not prime
break;
}
}
if($prime){
echo "PRIME";
}else{
echo "NOT PRIME";
}

?>

Output:
NOT PRIME

// when we assign value to n is 5 then output will be PRIME
--------------------------------------------------------------------------------------------




Other important posts for students of BCA/MCA/MCM/Msc/Bsc/MBA-IT/BE/BTech

Guidance for IT Students

Future of PHP Developer

PHP: Past, Today and Future

This blog is very helpful for IT Students and IT Freshers.


1 comment:

  1. I have read your blog it’s very attractive and impressive. I like it your blog.
    Click for this

    ReplyDelete

We are here to listen you, Comment your valueable opinion...!!!