Jump to content

GCD help needed


nicolette

Recommended Posts

what is wrong with this code? it keeps showing the message

"Warning: Division by zero in C:\xampp\htdocs\FinalPart3.php on line 34"

This is lines 33, 34, and 35:

while ($c = $num1 % $num2)
				{
					$num1 = $num2;

here is the entire code.

 

<?php 
if (!isset($_POST['SubmitPart3']))
	{
?>
<HTML>
<head>
<title> Final Part 3</title>
<body>
<h2>Please enter two number you wish to find the GCD of.</h2>
<form method="POST">
<table>
<tr>
<td>Nmber 1: </td>
<td> <input type="text" name="num1" /></td><br />
</tr>
<tr> 
<td>Number 2: </td>
<td> <input type="text" name="num2" /> </td>
</tr>
<tr>
<td><input type="submit" name="SubmitPart3" value="Find GCD" /></td>
</tr>
</table>
</form>
</body>
<?php
}
$num1 = ($_POST['num1']);
$num2 = ($_POST['num2']);
$c = 0;
	function gcd($num1, $num2)
		{
			while ($c = $num1 % $num2)
				{
					$num1 = $num2;
					$num2 = $c;
				}
				return $num2;
		}
echo "The GCD of $num1 and $num2 is " . gcd($num1,$num2);
?>code]

Link to comment
https://forums.phpfreaks.com/topic/206350-gcd-help-needed/
Share on other sites

You need to check whether the input is actually numeric, and you need to check whether either input is above 0.

 

is_numeric($input); // This function checks whether the contents of the variable is purely numerical (a number).

 

Then just check if the $input is greater than 0.

 

-cb-

Link to comment
https://forums.phpfreaks.com/topic/206350-gcd-help-needed/#findComment-1079454
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.