JavaScript Program to Convert Celsius to Fahrenheit
Last Updated :
28 Dec, 2023
Fahrenheit and centigrade are the two most used temperature scales and we often need to convert Celsius to Fahrenheit. In this article, we are going to learn how to Convert Celsius to Fahrenheit.
Fahrenheit
Fahrenheit is a scale of temperature. Its unit is the degree Fahrenheit (°F). This unit is named after physicist Daniel Gabriel Fahrenheit who first proposed this temperature scale in 1724. On the Fahrenheit scale, the water freezes at 32°F and boils at 212°F under normal conditions. Fahrenheit is also the U.S. customary measurement unit of temperature.
Celsius
Celsius is also a scale of temperature. Its unit is the degree Celsius (°C). This unit was named after astronomer Anders Celsius in 1948. On the Celsius scale, the water freezes at 0°C and boils at 100°C under normal conditions. Almost the whole world uses Celsius for temperature measurement aside from the U.S. Celsius is sometimes referred to as Centigrade.
The formula for converting the Celsius Scale to the Fahrenheit Scale is:
T(°F) = T(°C) × 9/5 + 32
Example:
Convert 10 degrees Celsius to degrees Fahrenheit:
T(°F) = 10°C × 9/5 + 32 = 50°F
Several methods can be used to Convert Celsius to Fahrenheit by using the above-mentioned formula, which are listed below:
In this approach, we are using a javascript custom function and applying the formula: Fahrenheit = (Celsius * 9/5) + 32. For a given temperature of 20°C.
Syntax:
function celcToFahr( n ) {
return ((n * 9.0 / 5.0) + 32.0);
}
Example: In this example, we are using the above-mentioned approach.
Javascript
function celcToFahr( n ) {
return ((n * 9.0 / 5.0) + 32.0);
}
const n = 20;
console.log(celcToFahr( n ));
|
Approach 2: Using the arrow function and template literals
In this approach, we are using the arrow function and template literals to convert Celsius to Fahrenheit. The arrow function applies the formula: Fahrenheit = (Celsius * 9/5) + 32. For a given temperature of 20°C.
Syntax:
const celcToFahr = (n) => `${n} Celsius is equal to ${(n * 9 / 5) + 32} Fahrenheit.`
Example: In this example, we are using the arrow function with the formula: Fahrenheit = (Celsius * 9/5) + 32.
Javascript
const celcToFahr = (n) =>
`${n} Celsius is equal to ${(n * 9 / 5) + 32} Fahrenheit.`;
const n = 20;
console.log(celcToFahr(n));
|
Output
20 Celsius is equal to 68 Fahrenheit.
Share your thoughts in the comments
Please Login to comment...