Jump to content
xisto Community
dvayne

Counting Variables?

Recommended Posts

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 by dvayne (see edit history)

Share this post


Link to post
Share on other sites

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.