Swift program to Convert Celsius to Fahrenheit


Fahrenheit is the commonly used temperature-measuring unit. In this scale, the freezing point and boiling point of the water are 32 degrees and 212 degrees. Whereas Celsius is also a temperature-measuring scale. In the Celsius scale, the freezing point and the boiling point of the water are 0 degrees and 100 degrees. So in Swift, we can convert Celsius to Fahrenheit using the following methods −

  • Using Formula

  • Using converted() method

Method 1: Using Formula

We can easily convert Celsius to Fahrenheit using the mathematical formula. It is the easiest way to convert Celsius to Fahrenheit.

Syntax

T(°F) = (T(°C) * 9/5) + 32

Here, T(°C) represent the temperature in Celsius and T(°F) represents the temperature in Fahrenheit.

Algorithm

  • Step 1 − Create a function which takes the Celsius value as input.

  • Step 2 − Inside the function, using the conversion formula convert Celsius to Fahrenheit.

  • Step 3 − Return the final result.

  • Step 4 − Create a variable to store the value of Celsius temperature.

  • Step 5 − Call the function and pass the temperature into it.

  • Step 6 − Display the result.

Example

In the following Swift program, we will convert Celsius to Fahrenheit. So we will create a function which takes the Celsius value as input and then converts Celsius to Fahrenheit using ((f-32)*5/9) and return the final result.

import Foundation
import Glibc

func celsiusToFahrenheit(c: Double) -> Double {
   return (c * 9/5) + 32
}

let tempurature: Double = 23.7
let result = celsiusToFahrenheit(c: tempurature)
print("\(tempurature)°C is equal to \(result)°F")

Output

23.7°C is equal to 74.66°F

Method 2: Using the converted() method

Swift provides good support to convert units without knowing the formula of the conversion with the help of an inbuilt Measurement structure. So to convert Celsius to Fahrenheit we create an instance of Measurement structure and then use the converted() function to convert Celsius to Fahrenheit.

Syntax

func converted(to: .celsius)

Here, we call this function on the Measurement instance and convert Celsius to Fahrenheit.

Algorithm

  • Step 1 − Create a function which takes the Celsius value as input.

  • Step 2 − Inside the function, using the conversion formula convert Celsius to Fahrenheit.

  • Step 3 − Return the final result.

  • Step 4 − Create a variable to store the value of Celsius temperature.

  • Step 5 − Call the function and pass the temperature into it.

  • Step 6 − Display the result.

Example

In the following Swift program, we will convert Celsius to Fahrenheit. So we will create an instance of Measurement using the Measurement() constructor and pass the value of Celsius temperature and the unit of measurement. Then we will convert Celsius to Fahrenheit using the converted() function and store the value in the new variable. And display the output.

import Foundation
import Glibc

let celsiusTemp = Measurement(value: 67.8, unit: UnitTemperature.celsius)
let fahrenheitTemp = celsiusTemp.converted(to: .fahrenheit).value

print("So, \(celsiusTemp) is equal to \(fahrenheitTemp) fahrenheit ")

Output

So, 67.8 °C is equal to 154.03999999999505 fahrenheit

Conclusion

So this is how we can convert Celsius to Fahrenheit. This conversion is very useful in various situations like weather Forecasts, Cooking, science and engineering, etc. Here both methods return the accurate result you can choose any of the methods according to your preference.

Updated on: 16-Jun-2023

503 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements