Program must be in JAVA.
Write a program that converts temperature from Celsius to Fahrenheit or vice versa, depending on user's input.
The formula for converting temperature in Celsius to Fahrenheit is,
Fahrenheit = (9/5) * Celsius + 32
The formula for converting temperature in Fahrenheit to Celsius is,
Celsius = (5/9) * (Fahrenheit – 32)
Results should be rounded to two decimal points, e.g., 92.56
import java.util.Scanner;
public class temp {
public static double
celsius_To_Fahrenheit(double degree_C)
{
double fahrenheit_Result =
(9*(degree_C) / 5) + 32;
return fahrenheit_Result;
}
public static double
fahrenheit_To_Celsius(double degree_F)
{
double celsius_Result = 5*(degree_F
- 32)/9;
return celsius_Result;
}
public static void init(){
Scanner scan = new
Scanner(System.in);
double result;
char cont= 'y';
char type;
while (cont!='q' &&
cont!='Q')
{
System.out.println("Please enter
the degree:");
double degree =
scan.nextDouble();
System.out.println("Now enter
the type of conversion (For Celsius, type 'C'. For Fahrenheit, type
'F')");
type = scan.next().charAt(0);
if(type!='c' &&
type!='C' && type!='f' && type!='F')
{
System.out.println("Error! You've
entered an invalid conversion type!");
System.out.println("Please enter a
valid selection (Type C for celsius and F for fahrenheit");
type = scan.next().charAt(0);
}
if (type == 'F' || type
=='f')
{
result =
fahrenheit_To_Celsius(degree);
System.out.println("The conversion
of "+ degree+"°F "+ "to Celsius is: " + Math.round(result * 100.0)
/ 100.0 + "°C");
}
if (type == 'C' || type
=='c')
{
result =
celsius_To_Fahrenheit(degree);
System.out.println("The conversion
of "+ degree+"°C "+ "to Fahrenheit is: " + Math.round(result *
100.0) / 100.0 + "°F");
}
System.out.println("Would you like
to quit? Type 'q' to quit or type any other letter to
continue:");
cont = scan.next().charAt(0);
}
scan.close();
}
public static void main(String[]
args)
{
init();
}
}
Code Image:
Output Image:
Get Answers For Free
Most questions answered within 1 hours.