Skip to content

Commit 0c4f472

Browse files
Add solution for Challenge 18 (#1193)
Co-authored-by: go-interview-practice-bot[bot] <230190823+go-interview-practice-bot[bot]@users.noreply.github.com>
1 parent d0b983a commit 0c4f472

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
fahrenheit := (celsius * (9.0 / 5.0)) + 32
23+
return Round(fahrenheit, 2)
24+
}
25+
26+
func FahrenheitToCelsius(fahrenheit float64) float64 {
27+
celsius := (fahrenheit - 32) * (5.0 / 9.0)
28+
return Round(celsius, 2)
29+
}
30+
31+
func Round(value float64, decimals int) float64 {
32+
precision := math.Pow10(decimals)
33+
return math.Round(value*precision) / precision
34+
}
35+

0 commit comments

Comments
 (0)