Write a Java program that converts Fahrenheit to Celsius and vice versa. Your pr
ID: 3572631 • Letter: W
Question
Write a Java program that converts Fahrenheit to Celsius and vice versa. Your program accepts as a parameter a Scanner holding a sequence of real numbers (temperature) followed by C or F for which temperature it is, and outputs the converted temperature. For example if the Scanner contains the following data:
35.6 C 70.2 F
25.0 F
Your program should produce the following output:
35.6 C = 96.1 F
70.2 F = 21.2 C
25.0 F = -3.9 C
These temperatures are converted using the following formulas:
C = 5/9 *( F – 32) F = (9/5) * C + 32
Explanation / Answer
package snippet;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Temparature {
public static void main(String args[]) throws IOException {
Double c, f;
Scanner sc=new Scanner(System.in);
while(true)
{
String line = sc.nextLine();
DecimalFormat df = new DecimalFormat("#.#");
String array[]=line.split(" ");
Double temp=Double.valueOf(array[0]);
switch (array[1])
{
case "F":
f=temp;
c = ((f - 32) * 5) / 9;
System.out.println( df.format(c)+" C");
break;
case "C":
c = temp;
f = (9 * c / 5) + 32;
System.out.println(df.format(f)+" F");
break;
}
}
}
}
==================================================================================
Output:
35.6 C
96.1 F
70.2 F
21.2C
25.0 F
-3.9C
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.