From b6275ba3637fd6b4e863f786bdbbb05ac3993cf9 Mon Sep 17 00:00:00 2001 From: "go-interview-practice-bot[bot]" <230190823+go-interview-practice-bot[bot]@users.noreply.github.com> Date: Fri, 31 Oct 2025 09:54:10 +0000 Subject: [PATCH] Add solution for Challenge 18 --- .../submissions/Johrespi/solution-template.go | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 challenge-18/submissions/Johrespi/solution-template.go diff --git a/challenge-18/submissions/Johrespi/solution-template.go b/challenge-18/submissions/Johrespi/solution-template.go new file mode 100644 index 00000000..0a3999b6 --- /dev/null +++ b/challenge-18/submissions/Johrespi/solution-template.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "math" +) + +func main() { + // Example usage + celsius := 25.0 + fahrenheit := CelsiusToFahrenheit(celsius) + fmt.Printf("%.2f°C is equal to %.2f°F\n", celsius, fahrenheit) + + fahrenheit = 68.0 + celsius = FahrenheitToCelsius(fahrenheit) + fmt.Printf("%.2f°F is equal to %.2f°C\n", fahrenheit, celsius) +} + +// CelsiusToFahrenheit converts a temperature from Celsius to Fahrenheit +// Formula: F = C × 9/5 + 32 +func CelsiusToFahrenheit(celsius float64) float64 { + // TODO: Implement this function + // Remember to round to 2 decimal places + return Round(celsius * (float64(9) / float64(5)) + float64(32), 2) +} + +// FahrenheitToCelsius converts a temperature from Fahrenheit to Celsius +// Formula: C = (F - 32) × 5/9 +func FahrenheitToCelsius(fahrenheit float64) float64 { + // TODO: Implement this function + // Remember to round to 2 decimal places + return Round((fahrenheit - float64(32)) * float64(5) / float64(9), 2) +} + +// Round rounds a float64 value to the specified number of decimal places +func Round(value float64, decimals int) float64 { + precision := math.Pow10(decimals) + return math.Round(value*precision) / precision +}