Page 1 of 1

help pls

Posted: Thu Jul 16, 2009 6:54 am
by bluekhille
pls help me to make a program using PHP

the problem is

the user must input a number, when it is even the number you input must be divide into half when it is odd number then the input number must be multiply into 3 then add 1. the process will stop when the answer is 1.

help pls :cry:

Re: help pls

Posted: Thu Jul 16, 2009 7:02 am
by onion2k

Code: Select all

while ($x > 1) { $x=($x%2==0)?$x/2:($x*3)+1; }

Re: help pls

Posted: Thu Jul 16, 2009 7:13 am
by bluekhille
its not working..

heres my code maybe there's something wrong with my code


heres my solution

Code: Select all

 
<html>
<body>
    
    <?PHP
    if (isset($_POST ["action"])){
    $Num = $_POST ["x"];
    
 
     while ($x > 1) { $x=($x%2==0)?$x/2:($x*3)+1; }
    echo "Answer is ". $Ans;
    }
    ?>
</body>
</html>
 
and here's my form

Code: Select all

 
<html>
<body>
    <form method = "POST" action = "compute.php">
    Initial Value is 
    <input type = "text" name = "x"> <BR>
    Next Value is<br>
      
    <input type = "submit" value = "compute" name = "action">
 
</body>
</html>
 

Re: help pls

Posted: Thu Jul 16, 2009 7:22 am
by jackpf
Your number is in $Num and you're using $x for the equation.

Re: help pls

Posted: Thu Jul 16, 2009 7:27 am
by bluekhille
i've change it but the problem now is that it only displays '1' as an answer. whatever numebr i use to input it always display 1.

Code: Select all

 
<html>
<body>
    
    <?PHP
    if (isset($_POST ["action"])){
    $x = $_POST ["x"];
    
 
     while ($x > 1) { $x=($x%2==0)?$x/2:($x*3)+1; }
    $Ans = $x;
    echo "Answer is ". $Ans;
    }
    ?>
</body>
</html>
 
 
<html>
<body>
    <form method = "POST" action = "compute.php">
    Initial Value is 
    <input type = "text" name = "x"> <BR>
    Next Value is<br>
      
    <input type = "submit" value = "compute" name = "action">
 
</body>
</html>

Re: help pls

Posted: Thu Jul 16, 2009 7:36 am
by jackpf
That's what you said you wanted - an answer of 1. You mean like this?

Code: Select all

<?php
    $x = $_POST['x'];
    
    while($x > 1)
    {
        $x = ($x % 2 == 0) ? $x / 2 : $x * 3 + 1;
        echo $x.'<br />';
    }
?>

Re: help pls

Posted: Thu Jul 16, 2009 7:41 am
by onion2k
bluekhille wrote:i've change it but the problem now is that it only displays '1' as an answer.
You need to read and understand the code before you can use it. You shouldn't just take something someone gives you, drop it into your code and run it. What if I'd posted something that wipes your server?

Re: help pls

Posted: Thu Jul 16, 2009 7:44 am
by bluekhille
before it will stop in 1 it should display the numbers before it arrive to 1.

for example if i enter number 9 then the output must be

enter number: 9
asnwer:
28
14
7
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1

Re: help pls

Posted: Thu Jul 16, 2009 7:47 am
by bluekhille
jackpf wrote:That's what you said you wanted - an answer of 1. You mean like this?

Code: Select all

<?php
    $x = $_POST['x'];
    
    while($x > 1)
    {
        $x = ($x % 2 == 0) ? $x / 2 : $x * 3 + 1;
        echo $x.'<br />';
    }
?>

this is the one im looking for thanks a lot! :D

Re: help pls

Posted: Thu Jul 16, 2009 7:47 am
by jackpf
This code will fix all your problems ;)

Code: Select all

<?php
    // removed by moderator
?>

And no problem. But you really should read stuff and understand it before you use it.

Re: help pls

Posted: Thu Jul 16, 2009 7:49 am
by bluekhille
onion2k wrote:
bluekhille wrote:i've change it but the problem now is that it only displays '1' as an answer.
You need to read and understand the code before you can use it. You shouldn't just take something someone gives you, drop it into your code and run it. What if I'd posted something that wipes your server?

that's why im asking for help because im not that good in php im only a beginner.. and my knowledge in php is not as good as yours. well tnx anyway.

Re: help pls

Posted: Thu Jul 16, 2009 7:52 am
by bluekhille
jackpf wrote:This code will fix all your problems ;)

Code: Select all

<?php
    // removed by moderator
?>

And no problem. But you really should read stuff and understand it before you use it.


:) such a genius huh..

step by step sir i will fully understand this language. im just confused with the functions that i should use this language is very very new to me that's why as of now im just relying with others help.

thanks alot again. :D

Re: help pls

Posted: Thu Jul 16, 2009 7:59 am
by jackpf
Well, I could have put it like this

Code: Select all

<?php
    // removed by moderator
?>
Which would have done the same thing. So you really can't be sure of what code does unless you...know what it does.

Anyway, no problem. And good luck.

Re: help pls

Posted: Thu Jul 16, 2009 7:36 pm
by Benjamin
Posting malicious code is not acceptable here guys. Don't do it again.

Re: help pls

Posted: Fri Jul 17, 2009 6:37 am
by jackpf
I was just kidding. My apologies.

Just trying to prove a point.