dvayne 0 Report post Posted January 12, 2008 (edited) Write a php script that will accept a positive integer, if it is even, divide it by 2 and if it is odd, multiply it by 3 and add 1. Repeat the process until the value is 1, printing out each value. Finally print out how many of these operations you performed. If the input value is less than 1, print error and perform an exit(); For example: Initial value: 9 Next value: 28 Next value: 14 Next value: 7 Next value: 22 Next value: 11 Next value: 34 Next value: 17 Next value: 52 Next value: 26 Next value: 13 Next value: 40 Next value: 20 Next value: 10 Next value: 5 Next value: 16 Next value: 8 Next value: 4 Next value:2 Final value:1 Number of steps: 19 Could you please modify this code? <?php$integer = intval($_GET['integer']);if ($integer < 1) { echo "Please Enter a Positive Integer";} else { echo "Entered Number is: ".$integer."<br />"; while($integer != 1 AND $integer >= 1) { $modulus = $integer%2; if ($modulus == 0) { $integer = $integer/2; echo $integer."<br />"; } else { $integer = $integer*3; $integer = $integer+1; echo $integer."<br />"; $integer = $integer/2;; echo $integer."<br />"; } }}?> Any help would be appreciated. Edited January 12, 2008 by dvayne (see edit history) Share this post Link to post Share on other sites
jlhaslip 4 Report post Posted January 12, 2008 what class is the assignment for?This reeks of homework. Share this post Link to post Share on other sites
gogoily 0 Report post Posted January 12, 2008 Try the following codes: <?php$step = 0;$integer = intval($_GET['integer']);if ($integer < 1) { echo "Please Enter a Positive Integer";} else { echo "Entered Number is: ".$integer."<br />"; while($integer != 1 AND $integer >= 1) { $modulus = $integer%2; if ($modulus == 0) { $integer = $integer/2; echo $integer."<br />"; } else { $integer = $integer*3; $integer = $integer+1; echo $integer."<br />"; } $step++; }}echo "Total step: ".$step;?> Share this post Link to post Share on other sites
dvayne 0 Report post Posted January 13, 2008 thanks whats the difference between your code with this code? <?php$step = 0;$integer = intval($_GET['integer']);if ($integer < 1) { echo "Please Enter a Positive Integer";} else { echo "Entered Number is: ".$integer."<br />"; while($integer != 1 AND $integer >= 1) { $modulus = $integer%2; if ($modulus == 0) { $integer = $integer/2; echo $integer."<br />"; } else { $integer = $integer*3; $integer = $integer+1; echo $integer."<br />"; $integer = $integer/2; echo $integer."<br />"; } $step++; }}echo "Total step: ".$step;?> Share this post Link to post Share on other sites
gogoily 0 Report post Posted January 17, 2008 See 16 and 17 line in your codes Share this post Link to post Share on other sites