15.
JavaScript Program to Create a Temperature Converter
ALGORITHM:
1. Start the program
2. Display input field and conversion buttons on the webpage
3. Accept temperature value entered by the user
4. Detect the button click event (Celsius → Fahrenheit or Fahrenheit → Celsius)
5. Read the input value using JavaScript (getElementById)
6. Perform the conversion using the appropriate formula and store the result
7. Display the result on the screen and stop
PROGRAM 15:
<!DOCTYPE html>
<html>
<body>
<h3>Temperature Converter</h3>
Enter Temperature:
<input type="number" id="t"><br><br>
<button onclick="cToF()">Celsius → Fahrenheit</button>
<button onclick="fToC()">Fahrenheit → Celsius</button>
<p id="out"></p>
<script>
function cToF(){
let c = [Link]("t").value;
let f = (c * 9/5) + 32;
[Link]("out").innerHTML = "Fahrenheit: " + f;
}
function fToC(){
let f = [Link]("t").value;
let c = (f - 32) * 5/9;
[Link]("out").innerHTML = "Celsius: " + c;
}
</script>
</body>
</html>
OUTPUT: