TypeScript: Convert Celsius to Fahrenheit

1. Introduction

Temperature conversion is a common task in various applications. In this blog post, we will walk through a TypeScript program that converts a temperature from Celsius to Fahrenheit.

2. Program Overview

The formula to convert Celsius to Fahrenheit is (Celsius * 9/5) + 32. Our program will define a function celsiusToFahrenheit that takes a number (Celsius temperature) and returns the equivalent Fahrenheit temperature.

3. Code Program

// Function to convert Celsius to Fahrenheit
function celsiusToFahrenheit(celsius: number): number {
    return (celsius * 9/5) + 32;
}

// Test the function
const celsiusTemperature = 25;
const fahrenheitTemperature = celsiusToFahrenheit(celsiusTemperature);
console.log(`${celsiusTemperature}°C is equal to ${fahrenheitTemperature}°F.`);

Output:

25°C is equal to 77°F.

4. Step By Step Explanation

1. We start by defining the celsiusToFahrenheit function. This function takes in a single argument: the temperature in Celsius.

2. Inside the function, we use the formula (Celsius * 9/5) + 32 to convert the Celsius temperature to Fahrenheit.

3. We then test the function with a sample Celsius value of 25.

4. The result, which is the temperature in Fahrenheit, is then logged to the console.

Comments