File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
challenge-18/submissions/inok94 Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 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+ // TODO: Implement this function
23+ // Remember to round to 2 decimal places
24+ f := celsius * 9 / 5 + 32
25+ return Round (f , 2 )
26+ }
27+
28+ // FahrenheitToCelsius converts a temperature from Fahrenheit to Celsius
29+ // Formula: C = (F - 32) × 5/9
30+ func FahrenheitToCelsius (fahrenheit float64 ) float64 {
31+ // TODO: Implement this function
32+ // Remember to round to 2 decimal places
33+ c := (fahrenheit - 32 ) * 5 / 9
34+ return Round (c , 2 )
35+ }
36+
37+ // Round rounds a float64 value to the specified number of decimal places
38+ func Round (value float64 , decimals int ) float64 {
39+ precision := math .Pow10 (decimals )
40+ return math .Round (value * precision ) / precision
41+ }
You can’t perform that action at this time.
0 commit comments