Conversion Methods
Completed
Write cToF and fToC. Read lines like C 100 or F 32 and print the converted temperature with 1 decimal and its unit.
Input
n, then n lines.
Output
n lines, e.g. 212.0 F.
Example
Input
3 C 100 F 32 C -40
Output
212.0 F 0.0 C -40.0 F
Editor
Java
Hints
One method per direction.
Solution explanation
Each method does one job, and its name says which.
Show solution
import java.util.Scanner;
public class Main {
static double cToF(double c) {
return c * 9 / 5 + 32;
}
static double fToC(double f) {
return (f - 32) * 5 / 9;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
String unit = sc.next();
double value = sc.nextDouble();
if (unit.equals("C")) {
System.out.printf("%.1f F%n", cToF(value));
} else {
System.out.printf("%.1f C%n", fToC(value));
}
}
}
}