ftpkid Posted November 21, 2012 Share Posted November 21, 2012 Hello, I'm a beginner and i just wrote an extremely simple program for solving a linear equation. It works, but i was wondering if you guys can take a look. Here it is: <?php$a = 5;$b = 10;$r = -($b/$a);if($a == 0 && $b == 0) {echo 'There are infinitely many solutions';}elseif($a>0 && $b>0) {echo $r;}elseif($a>0 && $b == 0) {echo 'X is equal to zero';}else {echo 'There are no solutions';}?> It's really, really basic and some parts aren't really correct(i guess). Please, tell me what you think. It isn't really complete, because the variable "a", might be a negative number. Also the code isn't exactly the most beautiful one... I didn't make an HTML form, so that the user can write the constants, on purpose. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 21, 2012 Share Posted November 21, 2012 You should check if $a is zero before doing the division. Link to comment Share on other sites More sharing options...
Ingolme Posted November 22, 2012 Share Posted November 22, 2012 Your code doesn't seem to be accurate for negative values of A and B. It seems you did note that in your post. Is this the equation you're trying to solve? x = -(b/a) Link to comment Share on other sites More sharing options...
ftpkid Posted November 22, 2012 Author Share Posted November 22, 2012 Your code doesn't seem to be accurate for negative values of A and B. It seems you did note that in your post. Is this the equation you're trying to solve? x = -(b/a) Yes, that's it. Link to comment Share on other sites More sharing options...
Ingolme Posted November 22, 2012 Share Posted November 22, 2012 There's a simpler way to do it: As long as $a is not 0, perform the division. If $a is 0 then check the value of $b. If $b is 0 then there are infinite possile answers, otherwise the answer is infinity. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now