dvayne
Jan 12 2008, 01:23 AM
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? 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.
Reply
jlhaslip
Jan 12 2008, 01:31 AM
what class is the assignment for? This reeks of homework.
Reply
gogoily
Jan 12 2008, 03:07 AM
Try the following codes: 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 />"; } $step++; } } echo "Total step: ".$step; ?>
Reply
dvayne
Jan 13 2008, 03:24 AM
thanks whats the difference between your code with this code? 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; ?>
Reply
gogoily
Jan 17 2008, 09:30 AM
See 16 and 17 line in your codes
Reply
Recent Queries:--
_get integer - 154.36 hr back. (1)
Similar Topics
Keywords : counting, variables,
- How Many Times Was The File Taken?
Counting downloads (5)
How Would I Go About Making A Simple "counting" Script?
(3) I plan on making a script for basic voting between different options, and I'd like to know what
PHP coding I would require. Basically, each choice will be as simple as this: CODE <form
method="post" action="process.php"> Best falsetto?<br><br>
<input type="radio" name="1"> Person A<br> <input
type="radio" name="2"> Person B<br> <input type="submit"
value="Submit"> </form> What PHP would be used to basically add 1 value to a....
Php Sessions And Post Variables Issues
My script dosent seem to work as intended (1) You can test it out for yourself at http://sonesay.trap17.com/application.php I've been
working on this page locally and it seems to be working fine but when I upload it to my trap17
account the post variables dont get saved properly. Fill in some fields and submit it, the form
will come up as a empty field yet when you resubmit it without any modifications and the data you
entered in orginally will now magically appear, resubmit it again and it will be gone. This is
really annoying as I have no clue why it would be doing this when it seems to work fine locally.....
C++ Lesson 2: Variables
Tutorial for C++ (0) As i said on other tutorial. This are tutorials from Gaianlurker on the New Engin3 forum. Alright,
this tutorial was going to be Variables and Functions, but that would just be massive so I broke it
down. Let's start with a basic program; CODE #include <iostream.h> //cin and cout
functions are here int main() { int a, b; cout<<"Welcome to
GaianLurker's basic math thingy."<<endl; cout<<"Please enter a
number."<<endl; cin>>a; cout<<".. and
another?"<<en....
Ti-basic: Solving Equations With Variables
Useful for Algebra (0) In this tutorial I will show you how to create a program that will solve a equation with variables
for you. There are some requirements though. Requirements A Ti-83 or better calculator
Knowledge of how to use the calculator Knowledge of where to find the functions A little Ti-BASIC
knowledge will be helpful Well lets get into it. Creating the Program First, we need to create
the program. Open up your program screen and create a new program. Name it something like EQSOLV.
Ok let's get started with the coding. WARNING: This program will use the followi....
Tricks With Php Variables
(10) You probably are thinking at this moment why I am posting some tricks with variables. Well, there
are many useful things a variable can do for us. For one they can actually be used to create simple
games. They also can be used to produce a random result in a page. Another great use is shoutboxes.
Well here are some cool variable effects. Adding Multiple Strings We can use strings to create
great effects. Example: CODE <?php $var1="Hello person.";
$var2=str_replace("person","world",$var1); echo $var1."
&....
How Do I
define variables in flash (3) were doing a course in ICT called multimedia, but i dont know how to define variables and whenever i
ask my teacher he always goes "were not that far yet" he doesn't even look at what i have
done, assumes I'm not finished, when i am, the only thing i need now is a scoring system....
Sending $_get[] Variables To An Application
(3) I'm trying to work out if it is possible to send variables to a application on server. E.g
send the variable of id which is equal to 10 to an application - test.exe?id=10 Any ideas to see if
this is possible?....
Second Php Tutorial - Variables
(8) If you haven't read the first one, read it by clicking here . I got a lot of positive
comments about my last tutorial. Do you guys think I should keep writitng these? Intro To PHP
Tutorial 2 - Variables Released 4/06/07 By Chris Feilbach aka GhostRider Contact Info: E-mail:
assembler7@gmail.com AIM: emptybinder78 Yahoo: drunkonmarshmellows Website:
http://www.ghostrider.trap17.com It has been a very long time since I have released a tutorial.
I've been quite busy working on my newest script for handling news (GAMES), and redesigning my
webpage. This tu....
How Do I Pass Php Variables From A Url To A Form?
I'm about read to tear my hair out with this one (5) Ok, what I need is for users to be able to select what address they want (e.g. mydomain + .com),
click submit that then sends the variables 'domain' and 'ext' through the URL e.g.
mywebsite.com/example.php?domain=mydomain&ext=com (the variables being mydomain and com) where they
are displayed in a form mail as "Domain Selected: mydomain.com" that can then be submitted and sent
to an email. In my form so far I have CODE <input name="domainname"
type="text" id="domainname" value ="<?php echo "$domain"; ?....
Destroying Variables In Javascript
Destroying variables in javascript (7) how do you delete the contents of a variable in javascript? Any ideas?....
Storing Session Variables Generated Dynamically
(3) i have many pool (quiz) modules on my site with the information taken from a database. The modules
are processed by the same function with different parameters and i need to save the pool's
status to prevent a user vote several times. I do this setting $_SESSION to 1 , variable
being the name of the quiz, which is taken from the database. So i get to having a code like this:
.................... $name= $row->quiz_name; $_SESSION =1 The problem is that if
i call $_SESSION from another part of the site (another page) the variable is al....
Php Application - Form Variables Not Being Passed
Tech Support did something to fix this problem. Very quick. (3) I'm trying to install an application, and it's not working. The programmer says that form
variables are not being passed between forms. This is a login application, so it has input like:
username: admin password: password The test (debug) program should return... LOGIN: Login
USERNAME: admin PASSWORD: password That will mean that variable are passed between forms. Since
right now it is not happening all you get is this: LOGIN: USERNAME: PASSWORD: Has anybody else
seen this? According to my programmer, this is a Server Admin (maybe configuration) problem ....
Php Variables And Url Forwarding
Tutorial -> PHP/URL forwarding (2) hi all this tutorial is about php get variable and url forwarding you can move variable in url with
php with out html form make one page with this code : CODE <?php $what =
$_GET['what']; $trap17 = $_POST['trap17']; echo
"Your name is : " . "$what" . "and Your Trap username is : " .
"$trap17"; ?> save this file with name "name.php" then upload name.php at
your hosting , then turn your browser at this url : http://yoursite.trap17.com/name.php?what=....
Session Variables
Sessions in PHP behaving strangely (4) Hi. I am part of a development team working in PHP and MySQL. The site is using SSL, and users
have to log to use the site. When users log in, their important details are retrieved from the
database and stored in session variables (functionality in the site is permissions specific).
Lately, sessions are "disappearing" for no apparent reason. Users will log in, and at some point
(the length of time will vary unpredicatably) the sessions will lose their value (the variables are
empty) and this causes the site to evict the user. This is very frustrating for the user b....
Converting Characters In A Variable To Individual Values In An Array
turning variables into arrays (2) Say I have a variable such as $nav_item and it had to contents Home . IE: CODE
$nav_item = 'Home'; How would I make so that $nav_item was an array and
had the following contents? CODE $nav_item = array ('h', 'o',
'm', 'e'); With the case changing (ie H would become h and U
would become u ) EDIT: Okay found out that I could change the case with
array_change_key_case ($nav_item, CASE_LOWER); ....
Passing Variables Through A Page?
(4) I have a form that people fill out. Depending on what they fill out on that form, they get different
things on the page they're taken to after hitting "submit". Information from both pages goes
into a database. However, I don't want to create a new row until the second page has been
filled out and submitted (taking the user to a third page). Is there any easy way to pass all the
variables from the first page through the second and to the third? I know I can stick it all in
hidden inputs, but I have more than 60 form items to process, and I'd like to not have....
The Difference In Variables?
(3) Hi y have a cuestion what is the diference or INT.VAR AND CHAR.VAR thanks /wink.gif' border='0'
style='vertical-align:middle' alt='wink.gif' /> ....
Transfer Variables To Another Php Script
(8) Hello, I've one registration page where the users fills in their information, is it possible to
trasnfer the things the fill in on the registration page to another script that does someting and
returnes something to the first page like true/false and then the registration gives an error
messange if the other php script returned false? Something like the script "activates" another
script that does something and returnes the result back to the original script. Best Regards ....
Console Vs Pc: Battle 334252234 And Counting...
Another generation, another war (12) Well, I hate to do this, but considering the next generation of consoles is now available ( atleast
1/3rd of them ), I have to ask; Whos Future Is Brighter: Console or PC gaming? It seems to me
that with every generation, console graphics improve nothing short of dramatically. Of course, thats
likely due to the fact that between each generation is a number of years with which to progress ones
technology. But the PC has had better graphics than every console since day 1. Until today. Well,
whatever day the Xbox 360 was released. Consoles now have better graphics t....
New Posts Aren't Counting
(8) Hello! I've been making posts today and yesterday in the Sig forum, but they are not
counting toward my hosting credits. Please help as soon as you can. I only have .96 left. Thanks
very much for your help!....
Counting Characters Of Textarea...
(3) good day webmasters... i am in need of a script that will count the inputted characters one will
enter... i mean the script must count the number of characters in a textarea as i am entering
characters to the textarea.. i know the logic but i cant translate it yet in PHP... so i hope you
can help me... thanks a lot in advance... /blink.gif' border='0' style='vertical-align:middle'
alt='blink.gif' /> ....
Dumping Fields Into Variables
(1) have a file that we are supposed to use as input. It looks like this: Code:
name:curriculum:gradyear:ssn:dob I'm going to read it in a buffer, but trying to find the logic
to seperate the fields and then place in a variable. I was thinking something like: Code:
if(ispunct(buf ) != ':') strcpy(s->name, buf); Any suggestions is greatly
appreciated!....
How Long Do $_post Variables Stay Stored?
(6) let's say that for whatever reason, let's say for a survey, i want to make a quiz like
w3schools.com has, only with PHP. i would want to put all the questions on different pages, also
like w3schools. could each of the submissions stay stored inside PHP's $_POST for 20
pages? if not, how could i get around it? PS. i really don't know anything about ASP other than
the fact that w3schools uses it. so please don't explain to me with ASP-specific terms.......
Help With Sql
Need values of some variables (1) i need to know this variables names: cfg : mysql-servername - ? cfg : mysql-username - ? cfg :
mysql-password - ? cfg : mysql-database-name - ? cfg : configuration-table - ? This
variables are to my chat script in php and to installe it i need to know what to put in each field.
Please help me with this?....
variables in and out
(5) how do you pass variables in a flash script, i know that to send them out you use the getURL
function but how would you pass them in?....
Looking for counting, variables,
|
|
Searching Video's for counting, variables,
|
advertisement
|
|