Jump to content

Division problem


nschmutz

Recommended Posts

I'm not sure how to create the code to represent a division problem.

 

Here is what I have so far in the code.

 

 

<html>

 

<head>

 

</head>

 

 

 

<form action="schmutz_extra_01.php" method="POST">

<p>Please enter an integer that is divisible by 3:</p>

<input type="text" name="number" value="<?php echo $_POST['integer'];?>"/></p>

<input type="submit" name="submit" value="Calculate!" />

</form>

 

 

<?php

 

function divisible($n)

{

$result = ($n) % 3;

return $result;

}

 

if (isset($_POST['integer']))

{

$integer = $_POST['integer'];

$div = divisible($integer);

echo "Your entered " . $integer . " and the result is " . $div . "</br>";

}

 

 

 

?>

 

</html>

Link to comment
https://forums.phpfreaks.com/topic/205425-division-problem/
Share on other sites

And what is your problem? What is the code not doing that you want it to or what is it doing that you don't want? You didn't ask a question and I'm not sure from looking at the code what you are trying to accomplish. But, I will say that your function divisible() is only returning the remainder of the number divided by three - is that what you wanted?

Link to comment
https://forums.phpfreaks.com/topic/205425-division-problem/#findComment-1075022
Share on other sites

To display result of the integer divided by 3 would be

 

$integer = $_POST['number'];
$integer = $integer / 3;
echo $integer;

 

To use the modulus operator to see IF the integer is divisible by 3, it would be

 

$integer = $_POST['number'];
if( ($integer % 3) == 0 ) {
     $divisible = "Divisible by 3";
} else {
     $divisible = "Not Divisible by 3"; 
}

Link to comment
https://forums.phpfreaks.com/topic/205425-division-problem/#findComment-1075056
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.