JavaScript

JavaScript Program to Convert Temperatures

Write a JavaScript program to convert temperatures to and from celsius, Fahrenheit [Formula: c/5 = f-32/9 [ where c = temperature in celsius and f = temperature in Fahrenheit ] Expected Output :
60°C is 140 °F
45°F is 7.222222222222222°C

<html>

<head>

<script language="javascript">

    function calculate()

    {

        var c = document.form1.celcius.value;

        var f = document.form1.farenheit.value;

        var resultF;

        var resultC;

        var result="";

        if(c!="")

        {

            resultF = c * 9 / 5 + 32;  

            result = c+'\xB0C is ' + resultF + ' \xB0F.';  

        }

        if(f!="")

        {

            resultC = (f - 32) * 5 / 9;

            result += '\n' + f +'\xB0F is ' + resultC + '\xB0C.';

        }

       

        alert(result);

    }

</script>

</head>

   

<body>

    <form name="form1">

        Celcius: <input type="text" name="celcius" /><br />

        Farenheit: <input type="text" name="farenheit" /><br />

        <input type="button" onclick="calculate()" value="calculate" />

    </form>

</body>

</html>
Back to top button