nicolette Posted July 1, 2010 Share Posted July 1, 2010 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 More sharing options...
Pikachu2000 Posted July 1, 2010 Share Posted July 1, 2010 You have no else{} in the conditional that checks whether the form has been submitted, therefore the calculation executes every time the script runs. Link to comment https://forums.phpfreaks.com/topic/206350-gcd-help-needed/#findComment-1079440 Share on other sites More sharing options...
nicolette Posted July 1, 2010 Author Share Posted July 1, 2010 ok got that fixed, these tired eyes are not helping matters any. so how would i go about getting the code to say that there is no common divisor? Link to comment https://forums.phpfreaks.com/topic/206350-gcd-help-needed/#findComment-1079450 Share on other sites More sharing options...
ChemicalBliss Posted July 1, 2010 Share Posted July 1, 2010 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 More sharing options...
nicolette Posted July 1, 2010 Author Share Posted July 1, 2010 ok but what about numbers that don't have a GCD like 8191 and 1023 how can I get it to print out that they don't have a GCD if they don't (normally returns 1) Link to comment https://forums.phpfreaks.com/topic/206350-gcd-help-needed/#findComment-1079471 Share on other sites More sharing options...
nicolette Posted July 1, 2010 Author Share Posted July 1, 2010 i got it Link to comment https://forums.phpfreaks.com/topic/206350-gcd-help-needed/#findComment-1079479 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.