Friday, July 12, 2024
HomeLanguagesJavascriptJavaScript Program to Convert Celsius to Fahrenheit

JavaScript Program to Convert Celsius to Fahrenheit

In this article, we are going to learn the conversion of Celsius to Fahrenheit by using javascript. by using the formula: Fahrenheit = (Celsius * 9/5) + 32.

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

There are several methods that can be used to Convert Celsius to Fahrenheit by using the above-mentioned formula, which are listed below:

  • Using the formula: Fahrenheit = (Celsius * 9/5) + 32, with custom function
  • Using the arrow function and template literals

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using the formula: Fahrenheit = (Celsius * 9/5) + 32, with custom function

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




// Javascript Program to convert Temperature 
// from Celsius to Fahrenheit
  
// function to convert Celsius
// to Fahrenheit
function celcToFahr( n ) {
    return ((n * 9.0 / 5.0) + 32.0);
}
  
// Driver code
const n = 20;
  
console.log(celcToFahr( n ));


Output

68

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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments