Forum : OOPS
Brief description  about Online courses   join in Online courses
View SARITA  DUBEY 's Profile

OOPS

please tell me what is wrong with this prgm:::

<?php

// define class

class Calculator
{

//class variables:


var $i;
//Class member functions or methods:
function Isprime($number1)
{

$this->i=$number1;
if($this->i < 2)
{
//return false;
echo "hello";
}
for($j=2;$j<=(($this->i)/2);$j )
{
if($this->i % $j ==0)
{
//return false;
echo "hi";
}
}
//return true;
echo "welcome";

}

}

$num1=$_POST['number1'];


//Create a class instance:
$calc = new Calculator();

//Call class methods:

$calc->Isprime($num1);

//Display output:


?>

i want to find whether number is prime or not..

please help me
Asked by SARITA DUBEY | Aug 3, 2010 |  Reply now
Replies (1)
View ajaykanth h c 's Profile
Hi..sorry for late reply..,

you had not put $j++ in for loop and also some important prime or not prime echo statements..., I've modified it as following..:



<?php

// define class

class Calculator
{

//class variables:


var $i;
//Class member functions or methods:
function Isprime($number1)
{

$this->i=$number1;
if($this->i < 2)
{
echo "Enter a number > 2";
return false;

}
for($j=2;$j<=(($this->i)/2);$j++ )
{
if($this->i % $j ==0)
{
echo $this->i. " is not a prime";
return false;

}
}
echo $this->i. " is prime";
return true;


}

}

$num1=$_POST['number1'];


//Create a class instance:
$calc = new Calculator();

//Call class methods:

$calc->Isprime($num1);

//Display output:

?>
NOTE:

as you might be knowing,make sure that you post the value/number to be checked with name="number1" through a form from another file to the above discussed file..,
Aug 14, 2010