help pls

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluekhille
Forum Commoner
Posts: 25
Joined: Thu Jul 16, 2009 6:49 am

help pls

Post 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:
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: help pls

Post by onion2k »

Code: Select all

while ($x > 1) { $x=($x%2==0)?$x/2:($x*3)+1; }
bluekhille
Forum Commoner
Posts: 25
Joined: Thu Jul 16, 2009 6:49 am

Re: help pls

Post 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>
 
Last edited by Benjamin on Thu Jul 16, 2009 7:33 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: help pls

Post by jackpf »

Your number is in $Num and you're using $x for the equation.
bluekhille
Forum Commoner
Posts: 25
Joined: Thu Jul 16, 2009 6:49 am

Re: help pls

Post 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>
Last edited by Benjamin on Thu Jul 16, 2009 7:33 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: help pls

Post 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 />';
    }
?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: help pls

Post 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?
bluekhille
Forum Commoner
Posts: 25
Joined: Thu Jul 16, 2009 6:49 am

Re: help pls

Post 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
bluekhille
Forum Commoner
Posts: 25
Joined: Thu Jul 16, 2009 6:49 am

Re: help pls

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: help pls

Post 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.
Last edited by Benjamin on Thu Jul 16, 2009 7:34 pm, edited 1 time in total.
Reason: Posting malicious code is not acceptable.
bluekhille
Forum Commoner
Posts: 25
Joined: Thu Jul 16, 2009 6:49 am

Re: help pls

Post 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.
bluekhille
Forum Commoner
Posts: 25
Joined: Thu Jul 16, 2009 6:49 am

Re: help pls

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: help pls

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: help pls

Post by Benjamin »

Posting malicious code is not acceptable here guys. Don't do it again.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: help pls

Post by jackpf »

I was just kidding. My apologies.

Just trying to prove a point.
Post Reply