
- Forum
- Programming Talk
- PHP
- php oops concept
php oops concept
This is a discussion on php oops concept within the PHP forums, part of the Programming Talk category; hello friends is anybody tell me how to make class to check prime number in php ... the input is ...
-
08-03-2010, 07:09 AM #1
- Join Date
- Aug 2010
- Answers
- 1
php oops concept
hello friends
is anybody tell me how to make class to check prime number in php ...
the input is given by user..
i tried a lot..but not able to do..
please help me..
-
03-10-2011, 04:34 AM #2
- Join Date
- Mar 2011
- Answers
- 3
<?php
// Checks for prime numbers
Class Demo
{
function IsPrime($Num)
{
$Result = null;
$No = 0;
for($CurrNum = 2; $CurrNum <= $Num; $CurrNum++)
{
for($Divisor = 2; $Divisor < $CurrNum; $Divisor++)
{
$Res = $CurrNum / $Divisor;
if($Res != 1 && intval($Res) == $Res)
{
$No = 1;
$Divisor = $CurrNum;
}
}
if($No != 1)
{
$Result = $CurrNum;
}
$No = 0;
}
// If the only divisor is the number itself, it's prime
if($Result == $Num)
{
return 1;
}
else
{
return 0;
}
}
}
// Check for primes, 0 to 100
$d= new Demo(); // object of class
for($i = 0; $i < 100; $i++)
{
echo $i." is a prime number? ".$d->IsPrime($i)."<br />";
}
?>
Last edited by admin; 03-10-2011 at 09:03 AM.

Reply With Quote





