Calculations with decimals need float64, and results are printed rounded.
What to build
A program that converts temperatures in Celsius to Fahrenheit and Kelvin.
Requirements
- Read n, then n temperatures in Celsius (decimal numbers).
- For each one print
C = F fahrenheit = K kelvin, where each number is shown with two decimals.
Input
Line 1: n. Line 2: n decimal numbers.
Output
n lines.
Constraints
1 ≤ n ≤ 20; -100 ≤ temperature ≤ 1000, with at most two decimals.
Example
Input:
3
0 100 -40
Output:
0.00 = 32.00 fahrenheit = 273.15 kelvin
100.00 = 212.00 fahrenheit = 373.15 kelvin
-40.00 = -40.00 fahrenheit = 233.15 kelvin
Why it works
float64 keeps the decimals. The formulas use 9.0 / 5.0 style decimal arithmetic, and %.2f rounds each number for printing.
Examples
Example 1 · Input
3 0 100 -40
Expected output
0.00 = 32.00 fahrenheit = 273.15 kelvin 100.00 = 212.00 fahrenheit = 373.15 kelvin -40.00 = -40.00 fahrenheit = 233.15 kelvin
Example 2 · Input
2 36.6 20.5
Expected output
36.60 = 97.88 fahrenheit = 309.75 kelvin 20.50 = 68.90 fahrenheit = 293.65 kelvin
Hints
Hint 1
Read the temperature as a float64.
Hint 2
Fahrenheit is c * 9 / 5 + 32 and Kelvin is c + 273.15; print both with %.2f.
Solution
Show one correct solution (try it yourself first)
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
for i := 0; i < n; i++ {
var c float64
fmt.Scan(&c)
f := c*9/5 + 32
k := c + 273.15
fmt.Printf("%.2f = %.2f fahrenheit = %.2f kelvin\n", c, f, k)
}
}