zafira Posted October 17, 2015 Share Posted October 17, 2015 (edited) Write a program which can convert a temperature in Fahrenheit to the corresponding temperature in Celcius and vice versa. To convert a temperature in Fahrenheit to Celsius, 32 degrees need to be subtracted and the result needs to be multiplied by (5/9). To convert a temperature in Celsius to Fahrenheit, divide the temperature by (5/9) and add 32 degrees to the result. this my coding <html> <head> <title>convert</title> </head> <body> <?php //Read the temperature $temp = "" ; //Read the 'direction' that is selected -- from Fahrentheit to Celcius ('ftc') or from Celcius to Fahrentheit ('clt'). $direction = "" ; if($direction == "ftc") { //Include the calculations that are needed. echo " $temp degrees Fahrentheit is <b>$temp_new</b> degrees Celsius " ; } else { //Include the calculations that are needed. echo " $temp degrees Celcius is <b>$temp_new</b> degrees Fahrentheit " ; } ?> <form action="convert.php" method="post"> <select name="direction"> <option value="ftc">Fahrenheit to Celsius </option> <option value="ctf">Celsius to Fahrenheit </option> </select> <input type="text" name="temp" size="6"/> <br/> <input type="Submit" value="Convert"/> </form> </body> </html> Edited October 17, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 17, 2015 Share Posted October 17, 2015 Did you not learn anything from the last two topics you posted? Also when posting code please wrap it in tags or click the <> in the editor. Quote Link to comment Share on other sites More sharing options...
Solution hansford Posted October 17, 2015 Solution Share Posted October 17, 2015 I just set it up on the same page for illustration purposes. <?php error_reporting(E_ALL); ini_set('display_errors',1); if (isset($_POST['direction']) && !empty($_POST['temp'])) { $direction = $_POST['direction']; $temp = $_POST['temp']; if ( ! is_numeric($temp)) { $error[] = "Input is not a valid number."; } if ( ! isset($error)) { if ($direction == "ftc") { $temp_new = convert_temp($temp); echo "$temp degrees Fahrentheit is <b>$temp_new</b> degrees Celsius " ; } else { $temp_new = convert_temp($temp,true); echo "$temp degrees Celcius is <b>$temp_new</b> degrees Fahrentheit " ; } } else { foreach ($error as $err) { echo $err . '<br />'; } } } function convert_temp($temp, $celcius=false) { if ($celcius) { $ret = ($temp * 9/5) + 32; } else { $ret = ($temp - 32) * 5/9; } return round($ret,1); } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>convert</title> </head> <body> <form action="" method="post"> <select name="direction"> <option value="ftc" selected>Fahrenheit to Celsius </option> <option value="ctf">Celsius to Fahrenheit </option> </select> <input type="text" name="temp" size="6"/> <br/> <input type="Submit" value="Convert"/> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
zafira Posted October 17, 2015 Author Share Posted October 17, 2015 thanks i really appreciate Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.