Dave Braunschweig

Temperature

// This program asks the user for a Fahrenheit temperature, 
// converts the given temperature to Celsius,
// and displays the results.
//
// References:
//     https://www.mathsisfun.com/temperature-conversion.html
//     https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html

func getFahrenheit() -> Double {
    var fahrenheit: Double
    
    print("Enter Fahrenheit temperature:")
    fahrenheit = Double(readLine(strippingNewline: true)!)!
    
    return fahrenheit
}

func calculateCelsius(fahrenheit: Double) -> Double {
    var celsius: Double
    
    celsius = (fahrenheit - 32) * 5 / 9
    
    return celsius
}

func displayResult(fahrenheit: Double, celsius: Double) {
    print(String(fahrenheit) + "° Fahrenheit is " + String(celsius) + "° Celsius")
}

func main() {
    var fahrenheit: Double
    var celsius: Double
    
    fahrenheit = getFahrenheit()
    celsius = calculateCelsius(fahrenheit:fahrenheit)
    displayResult(fahrenheit:fahrenheit, celsius:celsius)
}

main()

Output

Enter Fahrenheit temperature:
 100
100.0° Fahrenheit is 37.7777777777778° Celsius

References

License

Icon for the Creative Commons Attribution-ShareAlike 4.0 International License

Programming Fundamentals Copyright © 2018 by Dave Braunschweig is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book