Skip to content

Commit 994e54b

Browse files
Add solution for Challenge 18 by clgp-aint-cool (#1570)
Auto-merged after 2 days with all checks passing. PR: #1570 Author: @clgp-aint-cool
1 parent 932f889 commit 994e54b

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
func main() {
9+
// Example usage
10+
celsius := 25.0
11+
fahrenheit := CelsiusToFahrenheit(celsius)
12+
fmt.Printf("%.2f°C is equal to %.2f°F\n", celsius, fahrenheit)
13+
14+
fahrenheit = 68.0
15+
celsius = FahrenheitToCelsius(fahrenheit)
16+
fmt.Printf("%.2f°F is equal to %.2f°C\n", fahrenheit, celsius)
17+
}
18+
19+
// CelsiusToFahrenheit converts a temperature from Celsius to Fahrenheit
20+
// Formula: F = C × 9/5 + 32
21+
func CelsiusToFahrenheit(celsius float64) float64 {
22+
return Round(celsius * 9/5 +32,2)
23+
}
24+
25+
// FahrenheitToCelsius converts a temperature from Fahrenheit to Celsius
26+
// Formula: C = (F - 32) × 5/9
27+
func FahrenheitToCelsius(fahrenheit float64) float64 {
28+
// TODO: Implement this function
29+
// Remember to round to 2 decimal places
30+
return Round((fahrenheit-32)*5/9,2)
31+
}
32+
33+
// Round rounds a float64 value to the specified number of decimal places
34+
func Round(value float64, decimals int) float64 {
35+
precision := math.Pow10(decimals)
36+
return math.Round(value*precision) / precision
37+
}

0 commit comments

Comments
 (0)